Skip to content

Instantly share code, notes, and snippets.

@colin-han
Last active February 22, 2017 06:40
Show Gist options
  • Save colin-han/e7a10b840fd436f8c968baf219c25710 to your computer and use it in GitHub Desktop.
Save colin-han/e7a10b840fd436f8c968baf219c25710 to your computer and use it in GitHub Desktop.
Failed execute on MySql database
let Sequelize = require('sequelize');
let sequelize = new Sequelize('test', 'root', 'xA123456', {
host: 'localhost',
dialect: 'mysql',
});
let Product = sequelize.define('Product', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: Sequelize.STRING,
});
let Order = sequelize.define('Order', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
customerName: Sequelize.STRING,
});
Order.belongsTo(Product);
Product.hasMany(Order);
sequelize.sync({force: true})
.then(() => Product.create({name: 'a'}))
.then((p) => {
return p.createOrder({customerName: 'A'})
})
.then(() => {
return Product.findAll({
include: [
{
model: Order,
limit: 1,
attributes: ['id']
}
],
});
});
// Console output:
// Executing (default): INSERT INTO `Products` (`id`,`name`,`createdAt`,`updatedAt`) VALUES (DEFAULT,'a','2017-02-22 06:35:59','2017-02-22 06:35:59');
// Executing (default): INSERT INTO `Orders` (`id`,`customerName`,`createdAt`,`updatedAt`,`ProductId`) VALUES (DEFAULT,'A','2017-02-22 06:35:59','2017-02-22 06:35:59',1);
// Executing (default): SELECT `Product`.`id`, `Product`.`name`, `Product`.`createdAt`, `Product`.`updatedAt` FROM `Products` AS `Product`;
// Executing (default): SELECT `id` FROM `Orders` AS `Order` WHERE `Order`.`ProductId` IN (1) LIMIT 1;
// Unhandled rejection TypeError: Cannot read property 'push' of undefined
// at /Users/colinhan/temp/TestSequelize2/node_modules/sequelize/lib/associations/has-many.js:344:64
// at Array.forEach (native)
// at Model.<anonymous> (/Users/colinhan/temp/TestSequelize2/node_modules/sequelize/lib/associations/has-many.js:343:13)
// at Model.tryCatcher (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/util.js:16:23)
// at Promise._settlePromiseFromHandler (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/promise.js:510:31)
// at Promise._settlePromise (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/promise.js:567:18)
// at Promise._settlePromise0 (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/promise.js:612:10)
// at Promise._settlePromises (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/promise.js:691:18)
// at Async._drainQueue (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/async.js:133:16)
// at Async._drainQueues (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/async.js:143:10)
// at Immediate.Async.drainQueues (/Users/colinhan/temp/TestSequelize2/node_modules/bluebird/js/release/async.js:17:14)
// at runCallback (timers.js:651:20)
// at tryOnImmediate (timers.js:624:5)
// at processImmediate [as _immediateCallback] (timers.js:596:5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment