fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
151 stars 95 forks source link

Cannot delete relations #446

Open hotlabs opened 3 years ago

hotlabs commented 3 years ago

im have Relation protected static $_has_many = array( 'translations' => array( 'key_from' => 'id', 'model_to' => 'Model_Country_Translation', 'key_to' => 'country_id', 'cascade_save' => true, 'cascade_delete' => true, ) ); ---------------Model_Country_Translation protected static $_primary_key = array('country_id', 'language_code'); protected static $_properties = array( 'country_id', 'language_code', 'name', 'slug', 'description', 'meta_title', 'meta_description', 'meta_keywords', );

cascade delete not working $country = Model_Country::find($id); $country->delete(); Error Primary key on model Model_Country_Translation cannot be changed.

Please help me fix this

nickhagen commented 3 years ago

I may be misspeaking but for cascade delete to work you have to prefetch all relationships you want to delete, such as:

$country = Mode_Country::query()->related('translations')->where('id', $id)->get_one(); $country->delete();

On Wed, Mar 10, 2021 at 10:20 AM hotlabs notifications@github.com wrote:

im have protected static $_has_many = array( 'translations' => array( 'key_from' => 'id', 'model_to' => 'Model_Country_Translation', 'key_to' => 'country_id', 'cascade_save' => true, 'cascade_delete' => true, ) ); protected static $_primary_key = array('country_id', 'language_code'); protected static $_properties = array( 'country_id', 'language_code', 'name', 'slug', 'description', 'meta_title', 'meta_description', 'meta_keywords', );

cascade delete not working $country = Model_Country::find($id); $country->delete(); Error Primary key on model Model_Country_Translation cannot be changed.

Please help me fix this

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/fuel/orm/issues/446, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHOFW2ZPOMER2OXVFVJ3OTTC6E5NANCNFSM4Y6HWESQ .

WanWizard commented 3 years ago

The problem here is most likely the key mismatch.

The parent model defines "country_id" as the primary key of the related table, but the related model defines a compound primary key. So it won't delete, it will try to detach, and you get the error because it sets the keys to NULL.

To know for sure you'll need to check the backtrace.