更改数据库表的默认主键时出现问题
我正在使用 Ruby on Rails 3,我想将 SQL 数据库表的默认主键从 id
更改为 account_id
。 account_id 语句和行为应与 id
相同(自动递增、唯一,...)。
在我的迁移文件中,我尝试了以下操作:
create_table :users, :id => false do |t|
t.integer :account_id
...
t.timestamps
end
add_index :users, :account_id, :primary => true
但是当我尝试编辑“users”表时,使用 MySQL Workbench ,我收到此错误:
'users': table is not editable because there is no primary key defined for the table
那么,如何正确设置“用户”表的主键?
I am using Ruby on Rails 3 and I would like to change the default primary key for a SQL database table from id
to account_id
. The account_id
statement and behavior should be the same of id
(auto-increment, unique, ...).
In my migration file I tryed the following:
create_table :users, :id => false do |t|
t.integer :account_id
...
t.timestamps
end
add_index :users, :account_id, :primary => true
But using MySQL Workbench, when I try to edit the 'users' table, I get this error:
'users': table is not editable because there is no primary key defined for the table
So, how can I set properly the primary key for my 'users' table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://api.rubyonrails.org/classes/ActiveRecord /ConnectionAdapters/SchemaStatements.html#method-i-create_table
有一个 :primary_key 选项用于更改主键列
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table
There is a :primary_key option for changing the primary key column