Why is doctrine:schema:update trying to add same DB constraint again?

I have a question regarding the symfony 3.4 console command

doctrine:schema:update

I have updated from an older version, and part of the update process involves updating the DB this way, I had FOSUserBundle and they changed the table structure a bit. In addition to that change, symfony console also tries to execute the following alteration:

ALTER TABLE users_groups ADD CONSTRAINT FK_FF8AB7E0A76ED395 FOREIGN KEY (user_id) REFERENCES fos_user (id) ON DELETE CASCADE

My question is why. My DB already has that constraint since years back from a previous schema update.

enter image description here

When the console tries to run that query, naturally I’m halted by an error:

Error thrown while running command "doctrine:schema:update --force". Message: "An exception occurred while executing 'ALTER TABLE users_groups ADD CONSTRAINT FK_FF8AB7E0A76ED395 FOREIGN KEY (user_id) REFERENCES fos_user (id) ON DELETE CASCADE':

Why is it trying to add the same constraint twice, or what am I doing wrong? Shouldn’t it be able to detect that the constraint is already in there and if so skip that alteration?

——— UPDATE ——–

Or wait, what I already have is apparently not a constraint but an index… so then to another question, did doctrine not use foreign key constraints in previous versions, did it do all that by code?

But still I wonder why the alteration is denied…

14:59:46 ERROR [console] Error thrown while running command
"doctrine:schema:update –force". Message: "An exception occurred
while executing ‘ALTER TABLE users_groups ADD CONSTRAINT
FK_FF8AB7E0A76ED395 FOREIGN KEY (user_id) REFERENCES fos_user (id) ON
DELETE CASCADE’:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(webtools.#sql-1292_1b, CONSTRAINT FK_FF8AB7E0A76ED395 FOREIGN
KEY (user_id) REFERENCES fos_user (id) ON DELETE CASCADE)"
["exception" =>
DoctrineDBALExceptionForeignKeyConstraintViolationException {
…},"command" => "doctrine:schema:update –force","message" => """ An
exception occurred while executing ‘ALTER TABLE users_groups ADD
CONSTRAINT FK_FF8AB7E0A76ED395 FOREIGN KEY (user_id) REFERENCES
fos_user (id) ON DELETE CASCADE’:n n SQLSTATE[23000]: Integrity
constraint violation: 1452 Cannot add or update a child row: a foreign
key constraint fails (webtools.#sql-1292_1b, CONSTRAINT
FK_FF8AB7E0A76ED395 FOREIGN KEY (user_id) REFERENCES fos_user
(id) ON DELETE CASCADE) """]

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *