我正在使用 express + ionic + mysql + sequelize 来开发应用,现在我在续集中遇到了错误。
我有三张桌子
xx_product
xx_product_specification_value
xx_specification_value
他们之间的关系如下:
xx_product hasMany xx_product_specification_value
xx_specification_value hasMany xx_product_specification_value
xx_product_specification_value belongsTo xx_product
xx_product_specification_value belongsTo xx_specification_value
这是代码:
模型/ products.js
module.exports = function(sequelize, DataTypes){
return xx_product = sequelize.define('xx_product', {
price: {
type: DataTypes.DECIMAL
},
full_name: {
type: DataTypes.STRING
},
name: {
type: DataTypes.STRING
},
image: {
type: DataTypes.STRING
},
introduction: {
type: DataTypes.TEXT
},
xxSupplierId: {
type: DataTypes.BIGINT,
field: 'supplier'
},
xxBrandId: {
type: DataTypes.BIGINT,
field: 'brand'
}
},{
freezeTableName: true,
tableName: 'xx_product',
timestamps: false,
classMethods: {
associate: function(models) {
xx_product.belongsTo(models.xx_supplier, {
foreignKey: 'supplier'
})
xx_product.belongsTo(models.xx_brand, {
foreignKey: 'brand'
})
xx_product.hasMany(models.xx_product_specification_value)
}
}
})
}
模型/ product_specification_value.js
module.exports = function(sequelize, DataTypes){
return xx_product_specification_value = sequelize.define('xx_product_specification_value', {
xxProductId: {
type: DataTypes.BIGINT,
field: 'products'
},
xxSpecificationValueId: {
type: DataTypes.BIGINT,
field: 'specification_values'
}
},{
freezeTableName: true,
tableName: 'xx_product_specification_value',
timestamps: false,
classMethods: {
associate: function(models) {
xx_product_specification_value.belongsTo(models.xx_product, {
foreignKey: 'products'
})
xx_product_specification_value.belongsTo(models.xx_specification_value, {
foreignKey: 'specification_values',
})
}
}
})
}
模型/ specification_value.js
module.exports = function(sequelize, DataTypes){
return xx_specification_value = sequelize.define('xx_specification_value', {
name: {
type: DataTypes.STRING
},
image: {
type: DataTypes.STRING
},
xxSpecificationId: {
type: DataTypes.BIGINT,
field: 'specification'
}
},{
freezeTableName: true,
tableName: 'xx_specification_value',
timestamps: false,
classMethods: {
associate: function(models) {
xx_specification_value.hasMany(models.xx_product_specification_value)
xx_specification_value.belongsTo(models.xx_specification, {
foreignKey: 'specification'
})
}
}
})
}
路由/ product.js
var models = require('../models')
exports.show = function(req, res){
var id = req.params.id
models.xx_product.findOne({
where: {
id: id,
},
include: [
{
model: models.xx_product_specification_value,
include: [
{
model: models.xx_specification_value,
include: [
{
model: models.xx_specification
}
]
}
]
}
]
})
.then(function(product){
res.json(product)
})
}
当我在routes / products.js中调用show进行查询时,我在控制台中收到错误,如下所示:
Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR:
Unknown column 'xx_product_specification_values.id' in 'field list'
控制台中的sql查询如下:
Executing (default): SELECT `xx_product`.`id`, `xx_product`.`price`,
`xx_product`.`full_name`, `xx_product`.`name`, `xx_product`.`image`,
`xx_product`.`introduction`, `xx_product`.`supplier` AS
`xxSupplierId`, `xx_product`.`brand` AS `xxBrandId`,
`xx_product`.`supplier`, `xx_product`.`brand`,
`xx_product_specification_values`.`id` AS
`xx_product_specification_values.id`,
`xx_product_specification_values`.`products` AS
`xx_product_specification_values.xxProductId`,
`xx_product_specification_values`.`specification_values` AS
`xx_product_specification_values.xxSpecificationValueId`,
`xx_product_specification_values`.`products` AS
`xx_product_specification_values.products`,
`xx_product_specification_values`.`specification_values` AS
`xx_product_specification_values.specification_values` FROM
`xx_product` AS `xx_product` LEFT OUTER JOIN
`xx_product_specification_value` AS `xx_product_specification_values`
ON `xx_product`.`id` = `xx_product_specification_values`.`products`
WHERE `xx_product`.`id` = '1742';
我的问题是为什么它查询 xx_product_specification_values 而不是 xx_product_specification_value ,为什么它是复数?我已设置 freeszeTableName = true 并设置 tableName = xx_product_specification_value
答案 0 :(得分:0)
我对表之间的关系定义犯了一个大错误。正如你所看到的,xx_product和xx_specification_value之间的关系应该是many_to_many,所以在sequelize中我应该将它们定义如下:
模型/ product.js
xx_product.belongsToMany(models.xx_specification_value, {
through: {
model: models.xx_product_specification_value,
unique: false
},
foreignKey: "products"
})
模型/ specification_value.js
xx_specification_value.belongsToMany(models.xx_product, {
through: {
model: models.xx_product_specification_value,
unique: false
},
foreignKey: "specification_values"
})