Foreign Key error when I trying to delete a child row

182 views Asked by At

I know there is a lot of threads with this topic and I readed a lot of them, but I don't understand yet how to solve my problem.

I have 3 tables represented by the following entities classes:

Application:

     /**
     * @ORM\Entity(repositoryClass=ApplicationRepository::class)
     */
    class Application
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=32, unique=true)
         */
        private $name;

        ...
    }

User

    /**
     * @ORM\Entity(repositoryClass=UserRepository::class)
     */
    class User
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\OneToOne(targetEntity=Application::class, cascade={"persist"}, fetch="EXTRA_LAZY")
         * @ORM\JoinColumn(nullable=false)
         */
        private $application;

        ...
    }

UserToken

    /**
     * @ORM\Entity(repositoryClass=UserTokenRepository::class)
     */
    class UserToken
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity=User::class, inversedBy="userTokens", cascade={"persist"}, fetch="EXTRA_LAZY")
         * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
         */
        private $user;
    
        /**
         * @ORM\OneToOne(targetEntity=Application::class, cascade={"persist"}, fetch="EXTRA_LAZY")
         * @ORM\JoinColumn(nullable=false)
         */
        private $application;

        ...
    }

When a user close session (logout) I want to delete the row in UserToken table but without removing the user in User table and application in Application table, the problem is that always says:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (db`.`user`, CONSTRAINT `FK_8D93D649C6798DB` FOREIGN KEY (`application_id`) REFERENCES `application` (`id`))"

I remove the row from UserToken as follow:

$em = $this->getDoctrine()->getManager();
$em->remove($userToken);
$em->flush();

I tried a lot of things, I know that cascade={"persist"} is at php level and onDelete is at DB level. And I understand the problem too, but i don't know how to fix it.

What can I do? Thanks a lot.

Important information:

Symfony version: 5.2.0

Doctrine version: 2.7.1-DEV

Mysql: 10.1.40-MariaDB

Edit:

Sorry,this is the (important) output of show create table:

User:

 UNIQUE KEY `UNIQ_8D93D6493E030ACD` (`application_id`),
 CONSTRAINT `FK_8D93D649C6798DB` FOREIGN KEY (`application_id`) REFERENCES `application` (`id`)

UserToken:

UNIQUE KEY `UNIQ_BDF55A633E030ACD` (`application_id`),
 KEY `IDX_BDF55A63A76ED395` (`user_id`),
 CONSTRAINT `FK_BDF55A633E030ACD` FOREIGN KEY (`application_id`) REFERENCES `application` (`id`),
 CONSTRAINT `FK_BDF55A63A76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)

And the ENGINE=InnoDB

1

There are 1 answers

0
Adoc On BEST ANSWER

If someone else has this problem, I was able to fix it.

First of all the relationship between tables its wrong.

In User and UserToken tables it must to be:

    /**
     * @ORM\ManyToOne(targetEntity=Application::class, fetch="EXTRA_LAZY")
     * @ORM\JoinColumn(nullable=false)
     */
    private $application;

Second, I cleaned doctrine cache:

php bin/console doctrine:cache:clear-metadata
php bin/console doctrine:cache:clear-query
php bin/console doctrine:cache:clear-result

After that, I tried to update my schema (or create a new migration), but symfony always says that no change has been detected. So I remove the schema, recreate it and validate it but nothing changed.

In DEV environment

php bin/console doctrine:schema:drop
php bin/console doctrine:schema:create
php bin/console doctrine:schema:validate

To make the changes effective (in my case) the most important thing it was to run the command:

php bin/console cache:clear

After that symfony accepted my changes and I was able to update the schema with a migration perfectly.

Now I can remove a UserToken without removing User and Application associated.