Delete user with active resettoken raise ForeignKeyViolation
La suppression du compte quand aucun profile n'est actif ne fonctionne pas si l'utilisateurice à un resettoken
associé.
logs
Deleting user #####
Traceback (most recent call last):
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.ForeignKeyViolation: update or delete on table "user" violates foreign key constraint "resettoken_user_uuid_fkey" on table "resettoken"
DETAIL: Key (uuid)=(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) is still referenced from table "resettoken".
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/var/www/hiboo/venv/bin/flask", line 8, in <module>
sys.exit(main())
File "/var/www/hiboo/venv/lib/python3.9/site-packages/flask/cli.py", line 986, in main
cli.main()
File "/var/www/hiboo/venv/lib/python3.9/site-packages/flask/cli.py", line 567, in main
return super().main(*args, **kwargs)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/var/www/hiboo/venv/lib/python3.9/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/var/www/hiboo/venv/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
return f(get_current_context(), *args, **kwargs)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/flask/cli.py", line 407, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/var/www/hiboo/hiboo/cli.py", line 25, in tasks_loop
models.User.delete_unused()
File "/var/www/hiboo/hiboo/models.py", line 150, in delete_unused
db.session.delete(user)
File "<string>", line 2, in delete
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 2664, in delete
self._delete_impl(state, instance, head=True)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 2690, in _delete_impl
cascade_states = list(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/mapper.py", line 3230, in cascade_iterator
queue = deque(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/relationships.py", line 1983, in cascade_iterator
tuples = self._value_as_iterable(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/relationships.py", line 1957, in _value_as_iterable
x = impl.get(state, dict_, passive=passive)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/attributes.py", line 941, in get
value = self._fire_loader_callables(state, key, passive)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/attributes.py", line 977, in _fire_loader_callables
return self.callable_(state, passive)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/strategies.py", line 911, in _load_for_state
return self._emit_lazyload(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/strategies.py", line 1047, in _emit_lazyload
result = session.execute(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1660, in execute
) = compile_state_cls.orm_pre_session_exec(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/context.py", line 319, in orm_pre_session_exec
session._autoflush()
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 2257, in _autoflush
util.raise_(e, with_traceback=sys.exc_info()[2])
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 2246, in _autoflush
self.flush()
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 3383, in flush
self._flush(objects)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 3523, in _flush
transaction.rollback(_capture_exception=True)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 3483, in _flush
flush_context.execute()
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/unitofwork.py", line 456, in execute
rec.execute(self)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/unitofwork.py", line 667, in execute
util.preloaded.orm_persistence.delete_obj(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/persistence.py", line 343, in delete_obj
_emit_delete_statements(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/orm/persistence.py", line 1468, in _emit_delete_statements
c = connection._execute_20(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1631, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 332, in _execute_on_connection
return connection._execute_clauseelement(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1498, in _execute_clauseelement
ret = self._execute_context(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1862, in _execute_context
self._handle_dbapi_exception(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2043, in _handle_dbapi_exception
util.raise_(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "/var/www/hiboo/venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
(psycopg2.errors.ForeignKeyViolation) update or delete on table "user" violates foreign key constraint "resettoken_user_uuid_fkey" on table "resettoken"
[SQL: DELETE FROM "user" WHERE "user".uuid = %(uuid)s]
[parameters: {'uuid': '8d390ffa-5a53-47b9-a3d2-d117ced81f2f'}]
(Background on this error at: https://sqlalche.me/e/14/gkpj)
Une possibilité est de contourner ce problème c'est de Réécrire la réinitialisation de mot de passe av... (#87 - closed).
Edited by f00wl