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

Inserting duplicate column into table with unique constraint #4017

Closed
ackerdev opened this Issue Jun 26, 2015 · 7 comments

Comments

2 participants
@ackerdev
Contributor

ackerdev commented Jun 26, 2015

When attempting to insert a new record into a database with duplicate data that would violate a unique column constraint, I am getting this cryptic error that escapes the promise chain (cannot be caught via the query promise)...

Uncaught TypeError: Cannot read property 'msg' of undefined
      at readableAddChunk (_stream_readable.js:163:16)
      at Socket.Readable.push (_stream_readable.js:126:10)
      at TCP.onread (net.js:538:20)

Version 3.3.0
MSSQL Dialect

@janmeier

This comment has been minimized.

Member

janmeier commented Jun 28, 2015

Can you please post a complete example? Models, and code to represent the issue

@ackerdev

This comment has been minimized.

Contributor

ackerdev commented Jun 29, 2015

I don't know how to create a unique index via sequelize, as this is an existing database, but if you manually add one to Foo.email as below, this example should do:

var Foo = sequelize.define('Foo', {
  email: Sequelize.STRING
});

Foo.create({email: 'foo@bar.com'}).then(function(){
  return Foo.create({email: 'foo@bar.com'});
});
@ackerdev

This comment has been minimized.

Contributor

ackerdev commented Jun 29, 2015

Also, we have found that adding unique: 'uniqConstraintName' will actually cause the SequelizeUniqueConstraintError to be thrown as it should, rather than the error in the original post.

unique: true does not exhibit this behavior and still causes the uncaught TypeError.

@janmeier

This comment has been minimized.

Member

janmeier commented Jun 29, 2015

Fix coming up :)

I recommend using Promise.longStackTraces() in the future to debug this kind of problems - gives you an actual stack-trace of where the error comes from

@ackerdev

This comment has been minimized.

Contributor

ackerdev commented Jun 29, 2015

Thanks!

Promise.longStackTraces() doesn't help in this case though as the actual error originates from the .save() (or in this case .create()) operation; the error is not contained within the promise.

@janmeier

This comment has been minimized.

Member

janmeier commented Jun 29, 2015

Hmm, worked for me :)

@ackerdev

This comment has been minimized.

Contributor

ackerdev commented Jun 29, 2015

Ah, sorry, it would indeed work as you say in my pseudo-example because of the first promise... it just isn't the case for my real code with the issue as I wasn't needing to create the first record; it already existed before entering the offending code.

I suppose I can always add Promise.try() to enable longStackTraces in the future.

Thanks 😄

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