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

Closed
opened 2026-04-24 23:37:41 +02:00 by adam · 5 comments
Owner

Originally created by @MartinCa on GitHub (Jul 18, 2023).

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

Originally created by @MartinCa on GitHub (Jul 18, 2023). ### 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
adam added the bug label 2026-04-24 23:37:41 +02:00
adam closed this issue 2026-04-24 23:37:41 +02:00
Author
Owner

@MartinCa commented on GitHub (Jul 20, 2023):

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.

@MartinCa commented on GitHub (Jul 20, 2023): 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](https://github.com/advplyr/audiobookshelf/assets/2415208/03205ace-1988-4ca6-b719-61648c220da0) 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.
Author
Owner

@advplyr commented on GitHub (Jul 20, 2023):

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

@advplyr commented on GitHub (Jul 20, 2023): Ah yeah, makes sense. I'll fix this for the next patch
Author
Owner

@jhofker commented on GitHub (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!

@jhofker commented on GitHub (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!
Author
Owner

@advplyr commented on GitHub (Jul 24, 2023):

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

@advplyr commented on GitHub (Jul 24, 2023): Yeah I keep meaning to put together a better error system, it'll save time debugging in the long run.
Author
Owner

@advplyr commented on GitHub (Sep 10, 2023):

Fixed in v2.4.0

@advplyr commented on GitHub (Sep 10, 2023): Fixed in [v2.4.0](https://github.com/advplyr/audiobookshelf/releases/tag/v2.4.0)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf#1260