Skip to content

Instantly share code, notes, and snippets.

@manuelbieh
Last active October 19, 2015 23:35
Show Gist options
  • Save manuelbieh/a7a0aa14b3ef53e0bb78 to your computer and use it in GitHub Desktop.
Save manuelbieh/a7a0aa14b3ef53e0bb78 to your computer and use it in GitHub Desktop.
Sequelize hasMany error when used with limit
import Sequelize from 'sequelize';
let sequelize = new Sequelize('test', 'root', '', {
host: 'localhost',
dialect: 'mysql',
logging: console.log,
}
);
// DEFINE MODELS
let ProductImage = sequelize.define('productImage', {
filename: Sequelize.STRING,
});
let ProductCategory = sequelize.define('productCategory', {
title: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
});
let Product = sequelize.define('product', {
title: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
},
}, {
scopes: {
overview: {
include: [
{
model: ProductImage,
required: false,
as: 'images',
// separate: true,
limit: 1
},
{
model: ProductCategory,
required: false,
as: 'categories',
through: {
attributes: []
}
}
],
// separate: true
},
}
});
let ProductCategoryRelation = sequelize.define('productCategoryRelation', {
category_id: Sequelize.INTEGER,
product_id: Sequelize.INTEGER,
});
// SETUP ASSOCIATIONS
ProductCategory.belongsToMany(Product, {
foreignKey: 'category_id',
through: ProductCategoryRelation,
as: 'products'
});
Product.belongsToMany(ProductCategory, {
foreignKey: 'product_id',
through: ProductCategoryRelation,
as: 'categories'
});
ProductCategoryRelation.hasMany(Product, {
foreignKey: 'id'
});
ProductCategoryRelation.hasMany(ProductCategory, {
foreignKey: 'id'
});
Product.hasMany(ProductImage, {
onDelete: 'CASCADE',
as: 'images',
foreignKey: 'product_id'
});
// DROP TABLES
let p, c;
sequelize.query('SET FOREIGN_KEY_CHECKS = 0 ')
.then(() => ProductImage.drop())
.then(() => ProductCategoryRelation.drop())
.then(() => ProductCategory.drop())
.then(() => Product.drop())
// CREATE TABLES
.then(() => ProductCategoryRelation.sync())
.then(() => Product.sync())
.then(() => ProductCategory.sync())
.then(() => ProductImage.sync())
// ADD SOME DEMO DATA
.then(() => Product.create({title: 'Test Product'}))
.then((product) => {
p = product;
return ProductCategory.create({
title: 'Test Category'
});
})
.then((category) => {
c = category;
return ProductCategoryRelation.create({
product_id: p.id,
category_id: c.id
});
})
.then(() => {
return ProductImage.bulkCreate([
{filename: 'a.jpg', product_id: p.id},
{filename: 'b.jpg', product_id: p.id},
{filename: 'c.jpg', product_id: p.id},
{filename: 'd.jpg', product_id: p.id}
])
})
// GET DEMO DATA
.then(() => {
console.log('----\nThis works:\n----');
return Product.scope('overview').findAll()
})
.then((product) => {
console.log(JSON.stringify(product, null, 2))
})
.then(() => {
return ProductCategory.findById(c.id)
})
.then((category) => {
console.log('----\nThis does not:\n----');
return category.getProducts({
joinTableAttributes: [],
scope: 'overview',
});
})
.then((products) => {
console.log(JSON.stringify(products, null, 2));
})
.catch((err) => {
console.error(err);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment