开发者

Trouble on changing the default primary key for a database table

开发者 https://www.devze.com 2023-02-18 01:26 出处:网络
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-incre

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_开发者_StackOverflow社区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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消