如何删除 ID 字段上的隐式序列?

cod*_*joe 2 activerecord ruby-on-rails primary-key sequence

我已经看到我们可以通过这样做来避免在对象的 ID 上使用 SEQUENCE:

create_table :table_name, :id => false do |t|
  t.integer :id
  t.timestamps
end
Run Code Online (Sandbox Code Playgroud)

但是,如果我的表已经创建,并且我想从架构中删除“CREATE SEQUENCE table_name_id_seq”,我该如何在不删除表的情况下执行此操作?如果不可能,我想也可以,但我不想丢失表格内容。

Jef*_*key 5

您必须使用原始 SQL 来执行此操作。类似于以下内容:

def up
  ActiveRecord::Base.connection.execute "DROP SEQUENCE table_name_id_seq"
end
Run Code Online (Sandbox Code Playgroud)

http://www.postgresql.org/docs/9.1/static/sql-dropsequence.html