mysql 顺序化选择不存在的列

kb5ga3dv  于 2023-01-08  发布在  Mysql
关注(0)|答案(1)|浏览(115)

我做了一个简单的创建,但sequelize总是尝试选择一个既不存在也不使用的列“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)
    });
});

我的用户评分模型(_R):

'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
            }
        });

生成的查询:
正在执行(默认):从user_ratings中选择idfk_userfk_productratingcreatedAtupdatedAtuserIdproductId作为user_rating,其中user_rating. fk_user = 1;

8cdiaqws

8cdiaqws1#

由于你没有提供你的续集版本,不能确切地假设你的问题,但根据续集版本5.21.7你的用户评级模型是拼写错误

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});
};

您的associate方法应该与foreignKey而不是foreign_key一起使用。
这是最后一个关联函数

user_rating.associate = function (models) {
    user_rating.belongsTo(models.user, {foreignKey: 'fk_user', unique: false});
    user_rating.belongsTo(models.product, {foreignKey: 'fk_product', unique: false});
};

相关问题