A
A
Alexander Lashchevsky2020-04-30 17:57:08
Doctrine ORM
Alexander Lashchevsky, 2020-04-30 17:57:08

How to restore the database when one entity has both OneToMany and OneToOne connections to another?

Hello!

Given:
There are two entities, let's say User and Address (in fact, I have different entities, but for simplicity of the example): User has a relationship with Address twice: OneToMany (+ mirror ManyToOne from Address to User) and one more OneToOne.

OneToMany finds all the addresses that the user has (addresses).
OneToOne specifies only one of the addresses, which is the main address (main_address).

I will attach an example code in the comments to this question.

What's the problem:
Everything works fine until you have to do a restore from a backup of the data.

Since, due to the OneToOne relationship, a column containing the main_address_id with a link to this entry is added to the User entity table. In the table of the Address entity, there is a user_id column, respectively, since there is a ManyToOne relationship.

But when restoring from backup, the database gives an error, because it cannot set the main_address_id value in the User table, since the Address table does not exist yet (it has not been restored from backup).

It is also impossible to restore Address first and then User, since the user_id must be specified in the Address, which must already exist.

Question:
How to solve this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question