我做了一个简单的创建,但是后继总是尝试选择一列'userId',该列不存在或已被使用。
我进行了一些数据库检查,如果一切正常,sequelize应该只创建它。但我收到消息:“'字段列表'中的未知列'uId'”
我的路线:
router.post('/rate', [verifyAuth, verifyNotBanned], function (req, res, next) {
const productId = req.body.productId;
const user_rating = parseInt(req.body.rating);
// first look if user bought this product
Purchase.findOne({where: {fk_buyer: req.decoded.id, fk_product: productId}}).then((purchase) => {
if (!purchase) {
let errNotBought = new Error();
errNotBought.message = "you cant rate a product, which you not bought";
errNotBought.status = 403;
next(errNotBought);
}
// check if user already rated it
}).then(() => Rating.findAndCountAll({where: {fk_user: req.decoded.id}}))
.then((ratings) => {
if (ratings.count > 0) {
let errRated = new Error();
errRated.message = "you already rated this product";
errRated.status = 403;
next(errRated);
}
})
// save the rating
.then(() => Rating.create({
fk_user: req.decoded.id,
fk_product: productId,
rating: user_rating
})).then((rate) => {
res.send({status: true, data: rate})
}).catch((err) => {
next(err)
});
});
我的user_ratings模型:
'use strict';
module.exports = (sequelize, DataTypes) => {
const user_rating = sequelize.define('user_rating', {
fk_user: {
type: DataTypes.INTEGER,
onDelete: "SET NULL",
onUpdate: "NO ACTION",
references: {
model: 'users',
key: 'id'
}
},
fk_product: {
type: DataTypes.INTEGER,
allowNull: false,
onDelete: "CASCADE",
onUpdate: "NO ACTION",
references: {
model: 'products',
key: 'id'
}
},
rating: DataTypes.FLOAT
}, {});
user_rating.associate = function (models) {
user_rating.belongsTo(models.user, {foreign_key: 'fk_user', unique: false});
user_rating.belongsTo(models.product, {foreign_key: 'fk_product', unique: false});
};
return user_rating;
};
这是错误还是我做错了什么?
迁移文件:
return queryInterface.createTable('user_ratings', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
fk_user: {
type: Sequelize.INTEGER,
onDelete: "SET NULL",
onUpdate: "NO ACTION",
references: {
model: 'users',
key: 'id'
}
},
fk_product: {
type: Sequelize.INTEGER,
onDelete: "CASCADE",
onUpdate: "NO ACTION",
references: {
model: 'products',
key: 'id'
}
},
rating: {
type: Sequelize.FLOAT,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
生成的查询:
执行(默认):SELECT
id
,fk_user
,fk_product
,rating
,createdAt
,updatedAt
,userId
,productId
来自user_ratings
,user_rating
位于user_rating
。fk_user
= 1;