Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TypeError when running findOrCreate with some of the newest pg versions #8005

@ppeerttu

Description

@ppeerttu

What you are doing?

I try to run findOrCreate with where- and defaults- objects with the newest sequelize (4.4.2) and pg (7.0.2). Database doesn't contain the instance I'm running this with (which would be the 'Bar' with id 11 in the example).

const Sequelize = require('sequelize');
const sequelize = new Sequelize('seq_test', 'Perttu', null, {
  host: 'localhost',
  dialect: 'postgres',
  port: 5432
});

var Foo = sequelize.define('Foo', {
  id: { type: Database.DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
  content: Database.DataTypes.TEXT
});

var Bar = sequelize.define('Bar', {
  id: { type: Database.DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
  name: Database.DataTypes.STRING
});

Foo.belongsTo(Bar);

const bar = {
  name: 'Test Bar',
  id: 11
};

sequelize.sync()
  .then(() => Bar.findOrCreate({ where: { id: bar.id }, defaults: { id: bar.id, name: bar.name }})
  .then((result, created) => {
    console.log(result);
  }).catch(err => {
    console.error(err);
  }));

What do you expect to happen?

I expected that the new 'Bar' -instance would be created and inserted into the database, and returned to me without any errors.

What is actually happening?

Somehow there seems to be an undefined variable running through the code. The new instance is inserted into the database, but the code bumps into an error.

TypeError: Cannot read property '0' of undefined
    at query.catch.then.then.queryResult (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/sequelize/lib/dialects/postgres/query.js:112:17)
    at tryCatcher (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/Users/Perttu/PepronProjects/sequelizeTest/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

I run at this error with my other project when I updated dependencies to the latest versions (same versions as in this example). Previously it's been working fine, and when downgrading the node-postgres (pg) to older versions (i.e. 6.4.0), the example above works fine. Pg version 7.0.0 resuts to the same error.

Please note that this works fine when the database finds the object. If I run the same code again, the database can find the instance and returns it nicely. The problem occurs when the instance has to be created into the database during the transaction.

I know that there was some breaking changes that caused problems to sequelize when pg released the version 7 but I didn't find any issues related to this particular problem.

Dialect: postgres
Database version: 9.6
Sequelize version: 4.4.2

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