0
votes

I have a database with several one-to-many/many-to-one relationships. For example, I have table called Students, and a related table called StudentNotes. The StudentNotes table has a foreign key called student_id. I want the foreign key to have the constraint on delete = cascade.

I set up my Doctrine 2 entities with the property @JoinColumn(on="CASCADE") and updated the database schema. Unfortunately, whenever it does this, it sets the on delete to "restrict". What am I doing wrong?

Here's the relevant code from my Students entity:

/**
 * @var Collection Notes
 * 
 * @OneToMany(targetEntity="StudentNotes", mappedBy="student")
 * @JoinColumn(onCascade="DELETE")
 */

protected $notes;

And from StudentNotes:

 /**
 * @var \Entities\Students Student
 * 
 * @ManyToOne(targetEntity="Students", inversedBy="notes")
 * @OrderBy({"datetime"="DESC"})
 */

protected $student;

I've even tried adding all of the column information (i.e., name="student_id", referencedColumnName="id"), but nothing changes.

EDIT

I messed up when I originally wrote this: I wrote @JoinColumn(onCascade="DELETE"), when I meant to write @JoinColumn(onDelete="CASCADE"). Either way, this is not working properly: validate-schema fails because the database is not in sync with the schema.

2

2 Answers

0
votes

onCascade do not exists, you need instead onDelete="CASCADE".

@JoinColumn(onDelete="CASCADE")
0
votes

OK, I figured out what I'd forgotten to do!

I had neglected to go back to the command line and run the following command:

php doctrine.php orm:schema-tool:update --force

This did the trick