Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: SequelizeUniqueConstraintError during library scan and force re-scan #1932

Closed
MartinCa opened this issue Jul 18, 2023 · 5 comments
Closed
Labels
bug Something isn't working

Comments

@MartinCa
Copy link
Contributor

Describe the issue

During library scan audiobookshelf crashes and the following exception is shown in the log.

The directory with the book contains a desc.txt and reader.txt.
I do not have duplicates of this book.
I cannot find the book or the author when searching after audiobookshelf has restarted.
Other books seem to be scanned and handled fine.

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error
    at Database.<anonymous> (/node_modules/sequelize/lib/dialects/sqlite/query.js:185:27)
    at Database.serialize (<anonymous>)
    at /node_modules/sequelize/lib/dialects/sqlite/query.js:183:50
    at new Promise (<anonymous>)
    at Query.run (/node_modules/sequelize/lib/dialects/sqlite/query.js:183:12)
    at /node_modules/sequelize/lib/sequelize.js:315:28
    at async SQLiteQueryInterface.insert (/node_modules/sequelize/lib/dialects/abstract/query-interface.js:308:21)
    at async BookAuthor.save (/node_modules/sequelize/lib/model.js:2490:35)
    at async Function.create (/node_modules/sequelize/lib/model.js:1362:12)
    at async Function.fullCreateFromOld (/server/models/LibraryItem.js:85:11) {
  name: 'SequelizeUniqueConstraintError',
  errors: [
    ValidationErrorItem {
      message: 'bookId must be unique',
      type: 'unique violation',
      path: 'bookId',
      value: '8fbc6144-444e-41c8-b964-8c192e2fcfe2',
      origin: 'DB',
      instance: bookAuthor {
        dataValues: {
          id: '75b6ad89-97ce-40b1-a040-3604f955a2fa',
          authorId: 'c6194182-93bb-4074-ac31-44c262d30f1f',
          bookId: '8fbc6144-444e-41c8-b964-8c192e2fcfe2'
        },
        _previousDataValues: { authorId: undefined, bookId: undefined },
        uniqno: 1,
        _changed: Set(2) { 'authorId', 'bookId' },
        _options: {
          isNewRecord: true,
          _schema: null,
          _schemaDelimiter: '',
          attributes: undefined,
          include: undefined,
          raw: undefined,
          silent: undefined
        },
        isNewRecord: true
      },
      validatorKey: 'not_unique',
      validatorName: null,
      validatorArgs: []
    },
    ValidationErrorItem {
      message: 'authorId must be unique',
      type: 'unique violation',
      path: 'authorId',
      value: 'c6194182-93bb-4074-ac31-44c262d30f1f',
      origin: 'DB',
      instance: bookAuthor {
        dataValues: {
          id: '75b6ad89-97ce-40b1-a040-3604f955a2fa',
          authorId: 'c6194182-93bb-4074-ac31-44c262d30f1f',
          bookId: '8fbc6144-444e-41c8-b964-8c192e2fcfe2'
        },
        _previousDataValues: { authorId: undefined, bookId: undefined },
        uniqno: 1,
        _changed: Set(2) { 'authorId', 'bookId' },
        _options: {
          isNewRecord: true,
          _schema: null,
          _schemaDelimiter: '',
          attributes: undefined,
          include: undefined,
          raw: undefined,
          silent: undefined
        },
        isNewRecord: true
      },
      validatorKey: 'not_unique',
      validatorName: null,
      validatorArgs: []
    }
  ],
  parent: [Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: bookAuthors.bookId, bookAuthors.authorId] {
    errno: 19,
    code: 'SQLITE_CONSTRAINT',
    sql: 'INSERT INTO `bookAuthors` (`id`,`bookId`,`authorId`) VALUES ($1,$2,$3);'
  },
  original: [Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: bookAuthors.bookId, bookAuthors.authorId] {
    errno: 19,
    code: 'SQLITE_CONSTRAINT',
    sql: 'INSERT INTO `bookAuthors` (`id`,`bookId`,`authorId`) VALUES ($1,$2,$3);'
  },
  fields: [ 'bookId', 'authorId' ],
  sql: 'INSERT INTO `bookAuthors` (`id`,`bookId`,`authorId`) VALUES ($1,$2,$3);'
}

Steps to reproduce the issue

Not sure how to generally reproduce it but for me:

  1. Scan or force re-scan library
  2. Wait for crash

Audiobookshelf version

2.3.2

How are you running audiobookshelf?

Docker

@MartinCa MartinCa added the bug Something isn't working label Jul 18, 2023
@MartinCa
Copy link
Contributor Author

I have found the cause for the issue I believe, when looking at the old metadata for one of my audiobooks it has the same author listed twice for the same book:

"media": {
    "libraryItemId": "li_eyfqt8120b3fnsiec9",
    "metadata": {
      "title": "Sea Stories",
      "subtitle": "My Life in Special Operations",
      "authors": [
        {
          "id": "aut_4e5i9fvq9jm7c3pd7o",
          "name": "William H. McRaven"
        },
        {
          "id": "aut_4e5i9fvq9jm7c3pd7o",
          "name": "William H. McRaven"
        }
      ],

image

It seems if metadata is read from tags and they mention the same author twice then duplicate authors are added which the unique constraints in the database do not like.

@advplyr
Copy link
Owner

advplyr commented Jul 20, 2023

Ah yeah, makes sense. I'll fix this for the next patch

@advplyr advplyr added the awaiting release Issue is resolved and will be in the next release label Jul 20, 2023
@jhofker
Copy link

jhofker commented Jul 24, 2023

For errors like this, it'd be great if the system logged the book (title or path) it choked on for easier debugging.

Really glad this is fixed and hope to see it released soon!

@advplyr
Copy link
Owner

advplyr commented Jul 24, 2023

Yeah I keep meaning to put together a better error system, it'll save time debugging in the long run.

@advplyr
Copy link
Owner

advplyr commented Sep 9, 2023

Fixed in v2.4.0

@advplyr advplyr closed this as completed Sep 9, 2023
@advplyr advplyr removed the awaiting release Issue is resolved and will be in the next release label Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants