Joabe Anderson Thu Jun 20 14:10:00 -0400 2013

Subject: how to handle this exception

Fatal error: Uncaught exception 'ActiveRecord\DatabaseException' with message '23000, 1451, Cannot delete or update a parent row: a foreign key constraint fails (`herbalife`.`vendas`, CONSTRAINT `FK_vendas_produtos_id_prod` FOREIGN KEY (`produto`) REFERENCES `produtos` (`id_prod`))' in C:\wamp\www\sis\libraries\activerecord\lib\Connection.php on line 312.

ActiveRecord\DatabaseException: 23000, 1451, Cannot delete or update a parent row: a foreign key constraint fails (`herbalife`.`vendas`, CONSTRAINT `FK_vendas_produtos_id_prod` FOREIGN KEY (`produto`) REFERENCES `produtos` (`id_prod`)) in C:\wamp\www\sis\libraries\activerecord\lib\Connection.php on line 312


Megan McVey Thu Jul 18 18:29:42 -0400 2013

Looks like the `produto` field in `vendas` refers to the `id_prod` field of the `produtos` table.

You are trying to delete an entry in `produtos` that is still referenced in `vendas`.

If the id_prod you are trying to delete is, say, "15", then you will need to delete all the `vendas` entries with a `produto` of "15" before the foreign key constraint will let you delete the `produtos` entry.

You can do that with a before_destroy hook.

Or you can modify the constraint (if your database supports it) to do "ON DELETE CASCADE" (which will auto-delete all the entries in vendas when you delete the produtos entry).

Hope this helps

See also the comments in: http://www.phpactiverecord.org/boards/4/topics/945-delete-table-relations

(1-1/1)