我想在我的表格是通過軌道遷移如下定義主鍵IDRails的遷移:主鍵ID與unsigned int類型(10)
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT
我使用的MySQL數據庫。
我想在我的表格是通過軌道遷移如下定義主鍵IDRails的遷移:主鍵ID與unsigned int類型(10)
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT
我使用的MySQL數據庫。
只需使用#execute
即可在您的遷移中使用您需要的SQL。
execute "ALTER TABLE things MODIFY id UNSIGNED(10) NOT NULL AUTO_INCREMENT"
或者列尚不存在於所有:
execute "ALTER TABLE things ADD COLUMN id UNSIGNED(10) NOT NULL AUTO_INCREMENT PRIMARY KEY"
這應該很好地工作。我認爲在你的遷移中可以下降到純SQL,在很多情況下這是必要的。
我計算出這個
create_table things, :id => false do |t|
t.column :id, 'INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)'
t.string :name
...
OR
create_table things, :id => false do |t|
t.column :id, ID_COLUMN
t.string :name
...
其中一些配置/模塊定義,如果你喜歡,以避免自定義SQL其他遷移使用,也
ID_COLUMN在遷移中,這也將起作用:
create_table(:user, id: false, primary_key: :id) do |t|
t.primary_key :id, :unsigned_integer, null: false, default: :null, auto_increment: true
t.string :name
end
你有什麼特別的理由想要這樣做嗎? –
更好的表現,我還沒有決定自己:) –