I have 2 tables with association has_one (certification has_one bio)
when I call @object.destroy (@object is certification object) I get the
following error:
Mysql::Error: #23000Cannot delete or update a parent row: a foreign key
constraint fails (project/certification, CONSTRAINT FKD99554BA1D0B0B41 FOREIGN KEY (_key) REFERENCES bio (_key)):
DELETE FROM bio
WHERE _key = 2
On Tue, Aug 29, 2006 at 02:09:55PM +0200, Jens K. wrote:
DELETE FROM bio
How can I delete associated row without disabling foreign key checks?
uh oh, foreign key reference checking in MySQL, I can’t believe it man
you could try the before_destroy hook in certification, to delete the
associated object:
class Certification
before_destroy { |certification| certification.bio.destroy }
I just re-read your mail - the above won’t work either.
Seems your foreign key constraint is the other way around - it says
certification depends on bio, so you should say that in Rails, too,
i.e., place the :dependent setting at the other end of the relationship.
That’s why one should just place the model logic just into one
place…
On Tue, Aug 29, 2006 at 02:37:45PM +0200, Dmitry H. wrote:
Hrm… can’t get it working
what d u mean “place the :dependent setting at the other end of the
relationship”?
I was confused by your naming of columns and the resulting mysql error.
class Bio < ActiveRecord::Base
belongs_to :certification,
:class_name => “Certification”,
:foreign_key => “_key”
what do your tables look like ? From these classes, you should have a
‘_key’ column in the ‘bio’ table, referencing ‘certification._key’.
where in certification it’s the primary key, and in bio it’s a foreign
key pointing to the certification this bio belongs to.
But from this message:
Mysql::Error: #23000Cannot delete or update a parent row: a foreign
key
constraint fails (project/certification, CONSTRAINT FKD99554BA1D0B0B41 FOREIGN KEY (_key) REFERENCES bio
(_key)):
DELETE FROM bio
WHERE _key = 2
it looks as if there’s a ‘_key’ column in the certification table
referencing the ‘bio’ row that should get deleted. Is it possible you
specified the constraint in the opposite direction as intended ?
on a sidenote, that column naming doesn’t make sorting this out any
easier …
Actually my tables look like in the following dump below.
I think maybe I get this error because bio has other foreign keys, but I
just need to delete the bio entry without deleting records from
person_profile and user_account (as I understand now)
And no, I can’t accept that ‘convention over configuration’, because I
have ready database structure and can’t change it