I've created a table without a primary key (:id => false), but now it has come back to bite my ass.
我创建了一个没有主键的表(:id => false),但现在又回来咬我的屁股了。
My app is already in production and I can't just drop it and recreate another one.
我的应用程序已经投入生产,我不能只删除它并重新创建另一个。
Is there a way to run a migration to add another auto increment primary key column to my table?
有没有办法运行迁移以向我的表添加另一个自动增量主键列?
4 个解决方案
#1
39
The command to add a primary key in a migration is:
在迁移中添加主键的命令是:
add_column :my_table, :id, :primary_key
add_column:my_table,:id,:primary_key
However, the wording of your question suggests that your table already has an auto-increment column. Unless I'm mistaken, there are several DBMS that do not allow more than one auto-increment column on a table.
但是,您的问题的措辞表明您的表已经有一个自动增量列。除非我弄错了,否则有几个DBMS不允许在表上使用多个自动增量列。
If you DO already have an auto-increment column and you actually want to use that column as your primary key, just add the following to your model:
如果您已经拥有自动增量列,并且实际上想要将该列用作主键,只需将以下内容添加到模型中:
set_primary_key "my_existing_column"
set_primary_key“my_existing_column”
Or in more recent versions of Rails:
或者在更新版本的Rails中:
self.primary_key = "my_existing_column"
self.primary_key =“my_existing_column”
In the case that you already have an auto-increment column and you can't use that as the primary key, you may be out of luck.
如果您已经有一个自动增量列,并且您不能将其用作主键,那么您可能会失败。
#2
2
If for some reason you created a table with a custom id
field, but forgot to set id
as the primary key, you need to run a migration to create the primary key constraint. The following was tested against a PostgreSQL database:
如果由于某种原因您创建了一个带有自定义ID字段的表,但忘记将id设置为主键,则需要运行迁移以创建主键约束。以下针对PostgreSQL数据库进行了测试:
class AddPrimaryKeyConstraintToFoobars < ActiveRecord::Migration
def up
execute "ALTER TABLE foobars ADD PRIMARY KEY (id);"
end
def down
execute "ALTER TABLE foobars DROP CONSTRAINT foobars_pkey;"
end
end
#3
1
I know in mySQl that you can add a column that has a default increment value. If you add that then each row will have a unique int value (and any new row will get a int value 1 greater then the last row added)
我知道在mySQl中你可以添加一个具有默认增量值的列。如果你添加它,那么每一行将具有唯一的int值(并且任何新行将获得比添加的最后一行更大的int值1)
You could add this column and set it as a primary key.
您可以添加此列并将其设置为主键。
#4
0
Was this a join table that now needs to become a real model with a primary key? If so, your best bet is to create the new table and copy the data into it.
这是一个现在需要成为具有主键的真实模型的连接表吗?如果是这样,最好的办法是创建新表并将数据复制到其中。
#1
39
The command to add a primary key in a migration is:
在迁移中添加主键的命令是:
add_column :my_table, :id, :primary_key
add_column:my_table,:id,:primary_key
However, the wording of your question suggests that your table already has an auto-increment column. Unless I'm mistaken, there are several DBMS that do not allow more than one auto-increment column on a table.
但是,您的问题的措辞表明您的表已经有一个自动增量列。除非我弄错了,否则有几个DBMS不允许在表上使用多个自动增量列。
If you DO already have an auto-increment column and you actually want to use that column as your primary key, just add the following to your model:
如果您已经拥有自动增量列,并且实际上想要将该列用作主键,只需将以下内容添加到模型中:
set_primary_key "my_existing_column"
set_primary_key“my_existing_column”
Or in more recent versions of Rails:
或者在更新版本的Rails中:
self.primary_key = "my_existing_column"
self.primary_key =“my_existing_column”
In the case that you already have an auto-increment column and you can't use that as the primary key, you may be out of luck.
如果您已经有一个自动增量列,并且您不能将其用作主键,那么您可能会失败。
#2
2
If for some reason you created a table with a custom id
field, but forgot to set id
as the primary key, you need to run a migration to create the primary key constraint. The following was tested against a PostgreSQL database:
如果由于某种原因您创建了一个带有自定义ID字段的表,但忘记将id设置为主键,则需要运行迁移以创建主键约束。以下针对PostgreSQL数据库进行了测试:
class AddPrimaryKeyConstraintToFoobars < ActiveRecord::Migration
def up
execute "ALTER TABLE foobars ADD PRIMARY KEY (id);"
end
def down
execute "ALTER TABLE foobars DROP CONSTRAINT foobars_pkey;"
end
end
#3
1
I know in mySQl that you can add a column that has a default increment value. If you add that then each row will have a unique int value (and any new row will get a int value 1 greater then the last row added)
我知道在mySQl中你可以添加一个具有默认增量值的列。如果你添加它,那么每一行将具有唯一的int值(并且任何新行将获得比添加的最后一行更大的int值1)
You could add this column and set it as a primary key.
您可以添加此列并将其设置为主键。
#4
0
Was this a join table that now needs to become a real model with a primary key? If so, your best bet is to create the new table and copy the data into it.
这是一个现在需要成为具有主键的真实模型的连接表吗?如果是这样,最好的办法是创建新表并将数据复制到其中。