0

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.

mgluesenkamp
  • 529
  • 6
  • 19

1 Answers1

1

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

Carlos
  • 1,411
  • 15
  • 21
  • Ok, but where do I have to do this? I have no relation between users and access tokens. Can I simply add the relation from users to access tokens and define the cascade operations? – mgluesenkamp Nov 13 '17 at 10:36
  • There should be annotations in User entity and AccessToken entity. The rest is managed by FosOauthBundle. – Carlos Nov 13 '17 at 10:37