我在rails 2.3.9上使用ruby。 rake db:migrate成功通過。但是,當我運行rake db:migrate:redo來測試下行部分時,我收到錯誤消息。我使用Innodb與MySQL。無法刪除外鍵約束
class AddConstraints < ActiveRecord::Migration
def self.up
ActiveRecord::Base.connection.execute <<-EOS
ALTER TABLE venues
ADD CONSTRAINT FOREIGN KEY (city_id)
REFERENCES cities (id)
ON DELETE restrict
ON UPDATE cascade
EOS
end
def self.down
ActiveRecord::Base.connection.execute <<-EOS
ALTER TABLE venues DROP FOREIGN KEY (city_id)
EOS
end
end
我得到的錯誤信息是
You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right
syntax to use near '(city_id)' at line 1:
ALTER TABLE venues DROP FOREIGN KEY (city_id)
我明白了。然而,這意味着不同的人對外鍵的不同會有所不同。這意味着沒有乾淨的方式來使用紅寶石軌道解決這個問題。除非我動態地找到正確的foreign_key。有沒有辦法告訴MySQL創建具有特定名稱的外鍵,或者完全取決於MySQL? – 2010-09-10 16:46:40
不確定你的意思是「不同的人爲不同的名稱」。無論如何,您可以在創建外鍵時選擇其名稱,請參閱http://dev.mysql.com/doc/refman/5.0/zh-CN/create-table.html和http://dev.mysql.com /doc/refman/5.0/en/innodb-foreign-key-constraints.html(或您的MySQL版本的相應頁面)。 – titanoboa 2010-09-14 11:49:34