Last active
October 19, 2015 23:43
-
-
Save manuelbieh/b0ec0f41614cf9924b14 to your computer and use it in GitHub Desktop.
Sequelize hasMany error when used with limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Sequelize from 'sequelize'; | |
let sequelize = new Sequelize('test', 'root', '', { | |
host: 'localhost', | |
dialect: 'sqlite', | |
storage: __dirname + '/database.sqlite', | |
logging: console.log, | |
} | |
); | |
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, | |
}); | |
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' | |
}); | |
let p, c; | |
sequelize.query('SELECT 1+1') | |
.then(() => ProductImage.drop()) | |
.then(() => ProductCategoryRelation.drop()) | |
.then(() => ProductCategory.drop()) | |
.then(() => Product.drop()) | |
.then(() => ProductCategoryRelation.sync()) | |
.then(() => Product.sync()) | |
.then(() => ProductCategory.sync()) | |
.then(() => ProductImage.sync()) | |
.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} | |
]) | |
}) | |
.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