Skip to content

TypeError with sqlite when violating a unique function based index #10016

Closed

Description

@justinkalland

What are you doing?

Using the dialect sqlite and a function based unique index. When persisting to the database a TypeError is thrown when expecting a SequelizeUniqueConstraintError.

'use strict';

const Sequelize = require('sequelize');

const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: ':memory:'
});

const User = sequelize.define('User', {
  username: Sequelize.STRING,
  email: {
    type: Sequelize.STRING,
    unique: true
  }
});

User.sync().then(() => {
  
  return sequelize.query('CREATE UNIQUE INDEX lower_case_username ON users ((lower(username)))');
  
}).then(() => {
  
  return User.create({
    username: 'boby'
  });
  
}).then(() => {
  
  return User.create({
    username: 'boby'
  });
  
});

What do you expect to happen?

A SequelizeUniqueConstraintError to be thrown.

What is actually happening?

A TypeError is thrown that can't be caught as a rejection on the promise chain.

TypeError: Cannot read property 'replace' of undefined (../sequelize/lib/dialects/abstract/query.js:133:61)

Dialect: sqlite
Dialect version: 3.8.0
Database version: 4.0.2 (npm sqlite3 package)
Sequelize version: 5.0.0-beta.12
Tested with latest release: Yes 4.39.0

Activity

  1. justinkalland commented on Oct 10, 2018

    @justinkalland
    ContributorAuthor

    Issue starts here. As the comment says it expects the error to have a table.column and extract the field name from that:

    // Sqlite post 2.2 behavior - Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: table.x, table.y
    match = err.message.match(/UNIQUE constraint failed: (.*)/);
    if (match !== null && match.length >= 2) {
    fields = match[1].split(', ').map(columnWithTable => columnWithTable.split('.')[1]);
    }

    But in the case of an index function the error returned from sqlite looks like:

    [19] [SQLITE_CONSTRAINT_UNIQUE]  A UNIQUE constraint failed (UNIQUE constraint failed: index 'lower_case_username')

    Should I fix this by modifying the blob above to return something other than undefined for the field name? Or should I modify AbstractQuery.getUniqueConstraintErrorMessage() to handle a falsey field?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions