java - MySQL cascade on delete changes in wrong direction (eclipselink as JPA) -
i ran weird issue:
i have table constrainable
, table attribute
. within attribute
table specify constrainable
attribute
belongs foreign key constraint. added cascade on delete
foreign key constraint in attribute
table.
now if want delete attribute
constrainable
being deleted too.. should not happening or wrong? using method so:
public void remove(idbobject obj) throws dbexception { if (manager != null) { idbobject o = null; try { o = manager.getreference(obj.getclass(), obj.getprimarykey()); } catch (entitynotfoundexception e) { throw new dbexception("entity doesnt exist"); } manager.gettransaction().begin(); manager.remove(o); manager.gettransaction().commit(); return; } throw new dbexception("manager closed or null"); }
what reasons behaviour?
more detailed outline of db:
constrainable-table:
| id |
attribute-table
:
| id | constrainable-id | value | <--- here cascade on delete
defined
that impossible, , have described correctly, should not happening. since computers lie there must mistake in configuration provoking behaviour.
check foreign key correctly defined:
alter table attribute-table add constraint fk_attr_constr foreign key (constrainable-id) references constrainable-table (id) on delete cascade on update no action;
check constrainable-table table not reference anywhere attribute table, or other tables might end referencing attribute. (if have more 1 cascade careful how cascades propagate through schema)
lastly remove on delete cascade , see if same behavior happens, since cascade should happen when remove referenced row contrainable-table deletion of attribute should have no effect on constrianable-table.
as general rule, avoid cascade when possible unless 100% sure because maybe developer find problem when working on thing if not aware of cascade.
Comments
Post a Comment