Sequelize.js 一to-many關聯sequelize(關係/協會)子查詢
var City = sequelize.define('city', { countryCode: Sequelize.STRING });
var Country = sequelize.define('country', { isoCode: Sequelize.STRING });
// Here we can connect countries and cities base on country code
Country.hasMany(City, {foreignKey: 'countryCode', sourceKey: 'isoCode'});
City.belongsTo(Country, {foreignKey: 'countryCode', targetKey: 'isoCode'});
[國家] & [市]在DB,這樣的:
- Country-001
* City-001-1
* City-001-2
* City-001-3
- Country-002
* City-002-1
* City-002-2
* City-002-3
- Country-003
no city
- Country-004
* City-004-1
* City-004-2
* City-004-3
...
- Country-100
* City-100-1
* City-100-2
* City-100-3
我想查詢[國家]存在[城市],用偏移量&限制,使用ORM,而不是sql,如下所示:
Country.findAll({
where: {},
include: [],
offset: 0,
limit: 10
});
我不知道該怎麼做?
我嘗試了一些方法,他們不太好。 這種方式是不正確的,它會返回9 [國家]不是10,[國家003]真的不在結果中,但限制是不正確的。
Country.findAll({
where: {},
include: [mode: City, require: true],
offset: 0,
limit: 10
});
add require false and try –
但是,我不希望[Country]沒有[City]返回結果。 – YETI