symfonyfosuserbundlefosoauthserverbundle

Can not delete User with FOSOAuthBundle enabled


I try to delete a user in my symfony 3 app. I use the FOSOAuthBundle and the FOSUserBundle.

I tried this code:

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

But I get this error:

An exception occurred while executing 'DELETE FROM user WHERE id = ?' with params [1]:\n\nSQLSTATE[23000]:
Integrity constraint violation: 1451 Cannot delete or update a parent row:
a foreign key constraint fails (`database`.`access_token`, CONSTRAINT `FK_B6A2DD68A76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))

I have this configuration:

# FOSOAuthServerBundle configuration
fos_oauth_server:
    db_driver: orm
    client_class: OAuthBundle\Entity\Client
    access_token_class: OAuthBundle\Entity\AccessToken
    refresh_token_class: OAuthBundle\Entity\RefreshToken
    auth_code_class: OAuthBundle\Entity\AuthCode
    service:
        user_provider: fos_user.user_provider.username_email
        options:
            access_token_lifetime: 86400
            refresh_token_lifetime: 2.628e+6

If I get a access token the user field is always null though in the database the correct id is saved and it is possible to login with this token.


Solution

  • YOu need to configure cascade operations. You need to decide what do you want to happen on access_token table when you delete an user. You can delete de token or set the related user as null.

    More info here