【发布时间】:2019-02-05 16:44:01
【问题描述】:
我有 2 个模型 Country,City。
它们相互关联。城市属于国家。现在我想做的是获取国家查询中的城市列表以及分页的限制和偏移量(如果可能)。
如果我在下面这样做,它将列出一个国家/地区的所有城市。我需要做的是能够使用限制和偏移参数来限制城市。
Country.findById(1, {
include: [
{
model: City,
as: 'cities'
}
]
});
国家模式
module.exports = (sequelize, DataTypes) => {
let Country = sequelize.define('Country', {
id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
code: {type: DataTypes.STRING, allowNull: false, unique: true },
name: DataTypes.STRING
});
Country.associate = (models) => {
Country.hasMany(models.City, {as: 'cities'});
};
return Country;
}
城市模型
module.exports = (sequelize, DataTypes) => {
let City = sequelize.define('City', {
id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
name: {type: DataTypes.STRING, allowNull: false, unique: true},
});
City.associate = (models) => {
City.belongsTo(models.Country, {as: 'country'});
};
return City;
}
【问题讨论】:
标签: sequelize.js