has_one and :dependent => :destroy

uh oh, foreign key reference checking in MySQL, I can't believe it man :wink:

you could try the before_destroy hook in certification, to delete the
associated object:

class Certification
  before_destroy { |certification| certification.bio.destroy }
  
Jens

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...

Jens

Dmitry,

Just a thought 'convention over configuration'....

Can you not just do a straight forward rails relationship using
migrations?

class AddTables < ActiveRecord::Migration
  def self.up
    create_table :certifications, :force => true do |t|
      t.column :name, :string
      t.column :created_on, :date
      t.column etc...
    end

    create_table :bios, :force => true do |t|
      t.column :certification_id, :integer
      t.column :name, :string
      t.column :created_on, :date
      t.column etc...
    end
  end

  def self.down
    drop_table :certifications
    drop_table :bios
  end
end

Then in your models you should only need;

class Certification < ActiveRecord::Base
  has_one :bio, :dependent => :destroy

class Bio < ActiveRecord::Base
  belongs_to :certification

And let rails take care of the rest?

Hrm.. can't get it working :frowning:

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.

My classes look like following:

class Certification < ActiveRecord::Base
# before_destroy { |certification| certification.bio.destroy }
  has_one :bio,
          :class_name => "Bio",
          :foreign_key => "_key",
          :dependent => :destroy

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 ...

Jens