Not able to drop foreign key constraints
I am using ruby on rails 2.3.9. The rake db:migrate passed successfully. However when I run rake db:migrate:redo to test the down part I get error message. I am using mysql with Innodb.
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
The error 开发者_如何学运维message I am getting is
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)
First, you should omit the parentheses after FOREIGN KEY
. Second, you must tell mysql the name of the fk constraint you want to drop, not the name of the column affected by that constraint. If you run SHOW CREATE TABLE venues
, you can find out the name of the constraint - probably something like venues_ibfk_1
or similar. Use this name for the ALTER TABLE
statement, e.g. ALTER TABLE venues DROP FOREIGN KEY venues_ibfk_1
.
Maybe remove the parentheses from the statement? A quick google showed statements without parentheses being used in the MySQL documentation.
精彩评论