Skip to content

(sqlite3.IntegrityError) UNIQUE constraint failed: authors.name when update metadata with chinese homophone #3403

Description

@annProg

Describe the bug/problem
Two chinese author:

id name
36494 姚尧
40669 妖妖

after translate, they are all "Yao Yao". When edit metadata of book wit author "妖妖(40669)", wiil raise error:

(sqlite3.IntegrityError) UNIQUE constraint failed: authors.name

This is simple log:

[2025-05-26 02:22:38,366] ERROR {cps.editbooks:574} Database error: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
(sqlite3.IntegrityError) UNIQUE constraint failed: authors.name
[SQL: UPDATE authors SET name=?, sort=? WHERE authors.id = ?]
[parameters: ('妖妖', '妖妖', 36494)]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

Expect to update author "妖妖" with id 40669, but in fact, the program attempts to update id 36494.

To Reproduce

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Logfile

[2025-05-26 03:35:44,520] ERROR {cps.editbooks:574} Database error: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
(sqlite3.IntegrityError) UNIQUE constraint failed: authors.name
[SQL: UPDATE authors SET name=?, sort=? WHERE authors.id = ?]
[parameters: ('妖妖', '妖妖', 36494)]
(Background on this error at: https://sqlalche.me/e/20/gkpj)
Traceback (most recent call last):
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 945, in do_execute
    cursor.execute(statement, parameters)
sqlite3.IntegrityError: UNIQUE constraint failed: authors.name

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/calibre-web/cps/editbooks.py", line 464, in do_edit_book
    input_authors, author_change = handle_author_on_edit(book, to_save["authors"])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/calibre-web/cps/editbooks.py", line 1325, in handle_author_on_edit
    input_authors = prepare_authors(author_name, config.get_book_path(), config.config_use_google_drive)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/calibre-web/cps/editbooks.py", line 640, in prepare_authors
    .filter(db.Books.authors.any(db.Authors.name == renamed_author.name)).all()
                                                                          ^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2704, in all
    return self._iter().all()  # type: ignore
           ^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2858, in _iter
    result: Union[ScalarResult[_T], Result[_T]] = self.session.execute(
                                                  ^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2365, in execute
    return self._execute_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2230, in _execute_internal
    ) = compile_state_cls.orm_pre_session_exec(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/context.py", line 577, in orm_pre_session_exec
    session._autoflush()
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 3065, in _autoflush
    raise e.with_traceback(sys.exc_info()[2])
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 3054, in _autoflush
    self.flush()
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4353, in flush
    self._flush(objects)
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4488, in _flush
    with util.safe_reraise():
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4449, in _flush
    flush_context.execute()
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py", line 466, in execute
    rec.execute(self)
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py", line 642, in execute
    util.preloaded.orm_persistence.save_obj(
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py", line 85, in save_obj
    _emit_update_statements(
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py", line 912, in _emit_update_statements
    c = connection.execute(
        ^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1416, in execute
    return meth(
           ^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 523, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1638, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1843, in _execute_context
    return self._exec_single_context(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1983, in _exec_single_context
    self._handle_dbapi_exception(
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2352, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/lsiopy/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 945, 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)
(sqlite3.IntegrityError) UNIQUE constraint failed: authors.name
[SQL: UPDATE authors SET name=?, sort=? WHERE authors.id = ?]
[parameters: ('妖妖', '妖妖', 36494)]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

Expected behavior

update correct author id.

Screenshots

Image

Environment (please complete the following information):

  • OS: [ubuntu 24.04]
  • Python version: [3.12.3]
  • Calibre-Web version: [0.6.24 ]:
  • Docker container: [LinuxServer]:
  • Special Hardware: []
  • Browser: [Chrome]

Additional context
Add any other context about the problem here. [e.g. access via reverse proxy, database background sync, special database location]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions