将自动增量添加回 Rails 中的主键列

发布于 2024-07-14 18:20:42 字数 55 浏览 4 评论 0原文

我错误地从表的 id 字段中删除了自动增量选项。 谁能告诉我如何通过迁移重新插入自动增量选项?

By mistake I removed the autoincrement option from id field of my table. Can anyone tell me how I can reinsert the option of autoincrement back through migration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝色星空 2024-07-21 18:20:43

尝试:

change_column :my_table, :id, :primary_key

或者

my_table.change_column :id, :primary_key

某些 Rails 数据库适配器可能不允许您在主键上调用 change_column。 如果是这种情况,那么您始终可以调用 execute 直接使用 SQL 执行更改:

MySQL:

execute('ALTER TABLE "my_table" CHANGE "id" "id"
  bigint DEFAULT NULL auto_increment PRIMARY KEY')

PostgreSQL (方法 1):

max_id = execute(%%Q{SELECT id FROM "my_table" ORDER BY "id" DESC
  LIMIT 1}).to_a.first
execute(%%Q{CREATE SEQUENCE "my_table_id_seq" START #{max_id+1}})
execute(%%Q{ALTER TABLE "my_table" ALTER COLUMN "id"
  TYPE bigint})
execute(%%Q{ALTER TABLE "my_table" ALTER COLUMN "id"
  SET DEFAULT nextval('my_table_id_seq'::regclass)})
execute(%%Q{ALTER TABLE "my_table" ADD PRIMARY KEY("id")}) 

PostgreSQL (方法 2):

max_id = execute(%%Q{SELECT "id" FROM "my_table" ORDER BY "id" DESC
  LIMIT 1}).to_a.first
execute(%%Q{ALTER TABLE "my_table" RENAME COLUMN "id" TO "id_orig"})
execute(%%Q{ALTER TABLE "my_table" ADD COLUMN "id" bigserial NOT NULL})
execute(%%Q{UPDATE "my_table" SET "id"="id_orig"})
execute(%%Q{ALTER SEQUENCE "my_table_id_seq" RESTART #{max_id+1}})
execute(%%Q{ALTER TABLE "my_table" DROP COLUMN "id_orig"})

如果您不想使用 bigint/bigserial(64 位),使用 int(11)/integer/serial反而。

Try:

change_column :my_table, :id, :primary_key

or

my_table.change_column :id, :primary_key

Certain Rails database adapters may not let you call change_column on the primary key. If that is the case then you can always call execute to perform the change using SQL directly:

MySQL:

execute('ALTER TABLE "my_table" CHANGE "id" "id"
  bigint DEFAULT NULL auto_increment PRIMARY KEY')

PostgreSQL (method 1):

max_id = execute(%%Q{SELECT id FROM "my_table" ORDER BY "id" DESC
  LIMIT 1}).to_a.first
execute(%%Q{CREATE SEQUENCE "my_table_id_seq" START #{max_id+1}})
execute(%%Q{ALTER TABLE "my_table" ALTER COLUMN "id"
  TYPE bigint})
execute(%%Q{ALTER TABLE "my_table" ALTER COLUMN "id"
  SET DEFAULT nextval('my_table_id_seq'::regclass)})
execute(%%Q{ALTER TABLE "my_table" ADD PRIMARY KEY("id")}) 

PostgreSQL (method 2):

max_id = execute(%%Q{SELECT "id" FROM "my_table" ORDER BY "id" DESC
  LIMIT 1}).to_a.first
execute(%%Q{ALTER TABLE "my_table" RENAME COLUMN "id" TO "id_orig"})
execute(%%Q{ALTER TABLE "my_table" ADD COLUMN "id" bigserial NOT NULL})
execute(%%Q{UPDATE "my_table" SET "id"="id_orig"})
execute(%%Q{ALTER SEQUENCE "my_table_id_seq" RESTART #{max_id+1}})
execute(%%Q{ALTER TABLE "my_table" DROP COLUMN "id_orig"})

If you do not want to use bigint/bigserial (64-bit), use int(11)/integer/serial instead.

梓梦 2024-07-21 18:20:43

我没有检查其他版本,但在 Rails 5 上你可以只设置 auto_increment 选项:

change_column :table_name, :id, :int, null: false, unique: true, auto_increment: true

或者如果你想要一个 bigint:

change_column :table_name, :id, :bigint, null: false, unique: true, auto_increment: true

I didn't check other versions but on Rails 5 you can just set the auto_increment option:

change_column :table_name, :id, :int, null: false, unique: true, auto_increment: true

Or if you want a bigint:

change_column :table_name, :id, :bigint, null: false, unique: true, auto_increment: true
天涯离梦残月幽梦 2024-07-21 18:20:43

您的Postgres代码不起作用,不可能在ALTER TABLE语句中使用serial或bigserial。 PostgreSQL 的正确 SQL 是

ALTER TABLE table ALTER COLUMN id TYPE int 
ALTER TABLE table ALTER COLUMN id TYPE bigint

Your Postgres code does not work, it's impossible to use serial or bigserial in an ALTER TABLE statement. Correct SQL for PostgreSQL is

ALTER TABLE table ALTER COLUMN id TYPE int 
ALTER TABLE table ALTER COLUMN id TYPE bigint
伪装你 2024-07-21 18:20:43

这对我有用,创建了一个主列并为所有行提供了 id。

add_column :tags_posts_rel, :id, :primary_key

This worked for me, created a primary column and gave ids to all the rows.

add_column :tags_posts_rel, :id, :primary_key
要走就滚别墨迹 2024-07-21 18:20:43

对于那些来到这里(像我一样)努力弄清楚如何制作使用 bigint 而不是 int 的自定义 id 列的人code>,我没有意识到的是,对于 Rails 5.1 及更高版本,bigint 是 id

https://github.com/rails/rails/pull/26266

For those of you who came here (like I did) in an effort to figure out how to make a custom id column that uses bigint instead of int, what I didn't realize is that for Rails 5.1 and above, bigint is the default type for id

https://github.com/rails/rails/pull/26266

临走之时 2024-07-21 18:20:43

更改列

Rails5

change_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true

Rails6

change_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true

添加列
导轨5

add_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true

导轨6

add_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true

Change Column

Rails5

change_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true

Rails6

change_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true

Add Column
Rails5

add_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true

Rails6

add_column :table_name, :id, :int, null: false, unique: true, auto_increment: true,  primary_key: true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文