Skip to content

Loading…

DDC-2036: indexBy breaks cascade remove #2714

Closed
doctrinebot opened this Issue · 11 comments

1 participant

@doctrinebot

Jira issue originally created by user jebbench:

Adding the annotation indexBy causes the cascade annotation to be ignored:

/****
*

  • @var ArrayCollection
  • @ORM\OneToMany(targetEntity="LocationData", mappedBy="location", indexBy="name", cascade={"persist", "remove"}) */ protected $data;

Will cause an error when deleting the objects:

An exception occurred while executing 'DELETE FROM location WHERE id = ?' with params {"1":19306}:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (symfony.location*data, CONSTRAINT FK_2DF7462364D218E FOREIGN KEY (location*id) REFERENCES location (id))

Removing indexBy fixes the issue.

My class structure is:

Location has many LocationData
LocationData extends AbstractData

The name field I am trying to index by comes from the AbstractData class.

@doctrinebot

Comment created by @FabioBatSilva:

Hi James

Do you have a failing test case ?
Could you attach/paste your entity please ?

Thanks

@doctrinebot

Comment created by jebbench:

Entity exhibiting the behavior.

@doctrinebot

Comment created by @FabioBatSilva:

Hi James,
I can't reproduce,

Are you sure that you dont have more than one LocationData whith the same name ?

When working with indexBy the index columns must be unique into the collection.
I recommend that you add a unique constrain in the columns name.

If i'm wrong about it, please
Could you change the added test case and try to make it fails ?

Thanks

@doctrinebot

Comment created by jebbench:

Hi Fabio,

The test case uses a SQLite DB without any foreign keys so the issue wouldn't be apparent.

How would I run the test against a MySQL db?

I've also attached the SQL log for the script I'm running, it looks the same as the output from the testcase.

Cheers,JB

@doctrinebot

Comment created by jebbench:

SQL Log from script causing errors.

@doctrinebot

Comment created by @FabioBatSilva:

Hi James,

You must copy phpunit.xml.dist to phpunit.xml and change the database configurations.

I have attached the mysql log, the test case works fine.

@doctrinebot

Comment created by jebbench:

It appears that it only attempts to delete 68 LocationData rows but in the database there are 76.

@doctrinebot

Comment created by jebbench:

Okay, I've found the reason for this; the name isn't unique, some are duplicated.
Once the duplicates are removed there are 68 rows.
I'll add a unique index to the name column and hopefully this problem will go away.

Not sure whether you would consider this a bug.

Thanks for your help.

@doctrinebot

Comment created by @FabioBatSilva:

No problem, you are welcome :)

Actually isn't a bug, it is a documented behavior.

"Fields that are used for the index by feature HAVE to be unique in the database."

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/working-with-indexed-associations.html#working-with-indexed-assocations

@doctrinebot

Issue was closed with resolution "Can't Fix"

@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.