截断并重新启动一组表的标识
truncate and restart identity of a set of tables
我的应用程序使用 Postgresql
。
我需要从一组表中删除所有行(table1
到 table4
)并使用 rb 文件中的一个命令重新启动 id
。
在 Postgresql documentation 中,我发现使用 RESTART IDENTITY 的 TRUNCATE 将完成如下工作:
TRUNCATE table1, table2, table3, table4 RESTART IDENTITY;
根据 Whosebug 上的 How to restart id counting on a table in PostgreSQL after deleting some previous data?,我可以使用以下命令:
ActiveRecord::Base.connection.execute("TRUNCATE TABLE your_table_name RESTART IDENTITY")
因此将两个文档放在一起,使用以下命令是否正确:
ActiveRecord::Base.connection.execute("TRUNCATE table1, table2, table3, table4 RESTART IDENTITY")
考虑到 API dock 文档中 connection
方法被报告为已弃用或移动?
现在推荐的方法是使用 ActiveRecord::Base.connection_pool.with_connection
块。
ActiveRecord::Base.connection_pool.with_connection do |conn|
conn.execute("TRUNCATE table1, table2, table3, table4 RESTART IDENTITY")
end
我的应用程序使用 Postgresql
。
我需要从一组表中删除所有行(table1
到 table4
)并使用 rb 文件中的一个命令重新启动 id
。
在 Postgresql documentation 中,我发现使用 RESTART IDENTITY 的 TRUNCATE 将完成如下工作:
TRUNCATE table1, table2, table3, table4 RESTART IDENTITY;
根据 Whosebug 上的 How to restart id counting on a table in PostgreSQL after deleting some previous data?,我可以使用以下命令:
ActiveRecord::Base.connection.execute("TRUNCATE TABLE your_table_name RESTART IDENTITY")
因此将两个文档放在一起,使用以下命令是否正确:
ActiveRecord::Base.connection.execute("TRUNCATE table1, table2, table3, table4 RESTART IDENTITY")
考虑到 API dock 文档中 connection
方法被报告为已弃用或移动?
现在推荐的方法是使用 ActiveRecord::Base.connection_pool.with_connection
块。
ActiveRecord::Base.connection_pool.with_connection do |conn|
conn.execute("TRUNCATE table1, table2, table3, table4 RESTART IDENTITY")
end