Sequelize数据库报错列名不存在

Sequelize database error colum name does not exists

问题是 findAll 产品 select 的列不存在 "PermissionId"

不明白为什么sequelize会生成这个专栏

错误是:

Unhandled rejection SequelizeDatabaseError: column "PermissionId" does not exist at Query.formatError (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/sequelize/lib/dialects/postgres/query.js:366:16) at /home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/sequelize/lib/dialects/postgres/query.js:72:18 at tryCatcher (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/promise.js:547:31) at Promise._settlePromise (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/promise.js:604:18) at Promise._settlePromise0 (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/promise.js:649:10) at Promise._settlePromises (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/promise.js:725:18) at _drainQueueStep (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/async.js:93:12) at _drainQueue (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/async.js:86:9) at Async._drainQueues (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/async.js:102:5) at Immediate.Async.drainQueues [as _onImmediate] (/home/baptiste/IUT2/ProjetS3/code/M3301/node_modules/bluebird/js/release/async.js:15:14) at processImmediate (internal/timers.js:439:21)

迁移代码:

Utilisateurs 表

    'use strict';
    module.exports = {
        up: (queryInterface, Sequelize) => {
            return queryInterface.createTable('Utilisateurs', {
                id: {
                    allowNull: false,
                    autoIncrement: true,
                    primaryKey: true,
                    type: Sequelize.INTEGER
                },
                adresseMail: {
                    type: Sequelize.STRING
                },
                nomUtilisateur: {
                    type: Sequelize.STRING
                },
                dateDeNaissance: {
                    type: Sequelize.STRING
                },
                motDePasse: {
                    type: Sequelize.STRING
                },
                createdAt: {
                    allowNull: false,
                    type: Sequelize.DATE
                },
                updatedAt: {
                    allowNull: false,
                    type: Sequelize.DATE
                },
                idPermission: {
                    type: Sequelize.INTEGER,
                    references: {
                        model: {
                            tableName: 'Permissions',
                            schema: 'public'
                        },
                        key: 'id'
                    }
                }
            });
        },
        down: (queryInterface, Sequelize) => {
            return queryInterface.dropTable('Utilisateurs');
        }
    };

权限表

'use strict';
    module.exports = {
        up: (queryInterface, Sequelize) => {
            return queryInterface.createTable('Permissions', {
                id: {
                    allowNull: false,
                    autoIncrement: true,
                    primaryKey: true,
                    type: Sequelize.INTEGER
                },
                estRegatier: {
                    type: Sequelize.BOOLEAN
                },
                estJury: {
                    type: Sequelize.BOOLEAN
                },
                estOrganisateur: {
                    type: Sequelize.BOOLEAN
                },
                createdAt: {
                    allowNull: false,
                    type: Sequelize.DATE
                },
                updatedAt: {
                    allowNull: false,
                    type: Sequelize.DATE
                }
            });
        },
        down: (queryInterface, Sequelize) => {
            return queryInterface.dropTable('Permissions');
        }
    };
    ```
    'use strict';
    module.exports = {
        up: (queryInterface, Sequelize) => {
            return queryInterface.createTable('Permissions', {
                id: {
                    allowNull: false,
                    autoIncrement: true,
                    primaryKey: true,
                    type: Sequelize.INTEGER
                },
                estRegatier: {
                    type: Sequelize.BOOLEAN
                },
                estJury: {
                    type: Sequelize.BOOLEAN
                },
                estOrganisateur: {
                    type: Sequelize.BOOLEAN
                },
                createdAt: {
                    allowNull: false,
                    type: Sequelize.DATE
                },
                updatedAt: {
                    allowNull: false,
                    type: Sequelize.DATE
                }
            });
        },
        down: (queryInterface, Sequelize) => {
            return queryInterface.dropTable('Permissions');
        }
    };
    ```
    models code :
    **Utilisateur**
    ```'use strict';
    module.exports = (sequelize, DataTypes) => {
        const Utilisateurs = sequelize.define('Utilisateur', {
            adresseMail: DataTypes.STRING,
            nomUtilisateur: DataTypes.STRING,
            dateDeNaissance: DataTypes.STRING,
            motDePasse: DataTypes.STRING,
            idPermission: DataTypes.INTEGER
        }, {});
        Utilisateurs.associate = function (models) {
            Utilisateurs.belongsTo(models.Permission);
            Utilisateurs.hasMany(models.Licencie);
        };
        return Utilisateurs;
    };

权限

'use strict';

    module.exports = (sequelize, DataTypes) => {
        const Permission = sequelize.define('Permission', {
            estRegatier: DataTypes.BOOLEAN,
            estJury: DataTypes.BOOLEAN,
            estOrganisateur: DataTypes.BOOLEAN
        }, {});
        Permission.associate = function (models) {
            Permission.hasMany(models.Utilisateur);
        };
        return Permission;
    };

你可以这样试试:

module.exports = (sequelize, DataTypes) => {
    const Utilisateurs = sequelize.define('Utilisateur', {
        adresseMail: DataTypes.STRING,
        nomUtilisateur: DataTypes.STRING,
        dateDeNaissance: DataTypes.STRING,
        motDePasse: DataTypes.STRING,
        // idPermission: DataTypes.INTEGER **I belive you must remove this line!
    }, {});
    Utilisateurs.associate = function (models) {
        Utilisateurs.belongsTo(models.Permission, { foreignKey: 'idPermission'});
        Utilisateurs.hasMany(models.Licencie);
    };
    return Utilisateurs;
};

我尝试做的是为 table 指定 foreign_PK,sequelize 将默认尝试 PermissionId,但您的密钥是 idPermission。

请为我糟糕的英语道歉!

非常感谢 Carlos Filipe Braz! 我找到了,所以我需要在 class Permission 和 Utilisateur 中添加 { foreignKey: 'idPermission'} 那就是工作


module.exports = (sequelize, DataTypes) => {
    const Permission = sequelize.define('Permission', {
        estRegatier: DataTypes.BOOLEAN,
        estJury: DataTypes.BOOLEAN,
        estOrganisateur: DataTypes.BOOLEAN
    }, {});
    Permission.associate = function (models) {
        Permission.hasMany(models.Utilisateur, { foreignKey: 'idPermission'});
    };
    return Permission;
};
/*Utilisateur*/
'use strict';
module.exports = (sequelize, DataTypes) => {
    const Utilisateurs = sequelize.define('Utilisateur', {
        adresseMail: DataTypes.STRING,
        nomUtilisateur: DataTypes.STRING,
        dateDeNaissance: DataTypes.STRING,
        motDePasse: DataTypes.STRING
        /*idPermission: DataTypes.INTEGER*/
    }, {});
    Utilisateurs.associate = function (models) {
        Utilisateurs.belongsTo(models.Permission, { foreignKey: 'idPermission'});
        Utilisateurs.hasMany(models.Licencie);
    };
    return Utilisateurs;
};