您可以對where子句使用原始查詢。
例如,假設您的Product
模型定義如下:
module.exports = (sequelize, DataTypes) => sequelize.define('products', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
}
}, {
tableName: 'products',
freezeTableName: true,
underscored: true,
classMethods: {
associate: models => {
models.products.hasMany(models.medias, {
foreignKey: 'mediable_id',
constraints: false,
scope: {
mediable: 'product'
}
});
}
}
});
和你Media
模型定義爲:
module.exports = (sequelize, DataTypes) => sequelize.define('medias', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
},
mediable: {
type: DataTypes.STRING,
allowNull: true
},
mediable_id: {
type: DataTypes.INTEGER,
allowNull: true
}
}, {
tableName: 'medias',
freezeTableName: true,
underscored: true,
classMethods: {
associate: models => {
models.products.belongsTo(models.products, {
foreignKey: 'id',
constraints: false
});
}
}
});
,那麼你可以查詢它是這樣的:
sequelize.sync().then(() => Promise.all([
// Prepopulate Data
models.products.upsert({
id: 1,
name: 'Product A'
}),
models.products.upsert({
id: 2,
name: 'Product B'
}),
models.products.upsert({
id: 3,
name: 'Product C'
}),
models.medias.upsert({
id: 1,
name: 'Media A',
mediable: 'item',
mediable_id: 1
}),
models.medias.upsert({
id: 2,
name: 'Media B',
mediable: 'product',
mediable_id: 1
}),
models.medias.upsert({
id: 3,
name: 'Media C',
mediable: 'product',
mediable_id: 1
}),
models.medias.upsert({
id: 4,
name: 'Media D',
mediable: 'product',
mediable_id: 2
}),
models.medias.upsert({
id: 5,
name: 'Media E'
})
])).then(() => models.products.findAll({
where: ["medias.mediable_id IS NULL OR medias.mediable != 'product'"],
include: [{
model: models.medias,
required: false,
}]
})).then(products => {
// The rest of your logic here...
});