table 未创建关联一对多续​​集节点

table association not created One-To-Many sequelize node

我正在为我的数据库创建关联。但是她并没有创建所有的协会,例如,她没有 link 带有工资单的合同。我尝试了数据库中的选项,但它们没有帮助。告诉我我可以尝试做什么以及我做错了什么?

创建关联

db.Contracts.hasMany(db.Payrolls, {
        foreignKey: {
            name: 'contract_id',
            sourceKey: 'id',
        }
    })
    db.Payrolls.belongsTo(db.Contracts, {
        foreignKey: {
            name: 'contract_id',
            targetKey: 'id',
        }
    })

创建合同

module.exports = (sequelize, DataTypes) => {
    const Contracts = sequelize.define("Contracts", {
        id: {
            type: DataTypes.STRING,
            allowNull: false,
            primaryKey: true,
            autoIncrement: false
        },
        month_pay: {
            type: DataTypes.INTEGER, 
            allowNull: false
        }
    }, { timestamps: false });
    
    return Contracts;
};

创建工资单

module.exports = (sequelize, DataTypes) => {
    const Payrolls = sequelize.define("Payrolls", {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false,
            autoIncrement: true
        },
        pay_date: {
            type: DataTypes.DATE,
            allowNull: false,
        },
        amount: {
            type: DataTypes.INTEGER, 
            allowNull: false
        }
    }, { timestamps: false });

    return Payrolls
};

App: DbShema

例如工作协会:

module.exports = (db) => {

    //#region One-To-Many Contracts->Courses
    db.Courses.hasMany(db.Contracts, {
        foreignKey: {
            name: 'course_name',
            sourceKey: 'name',
            primaryKey: true,
        }
    })
    db.Contracts.belongsTo(db.Courses, {
        foreignKey: {
            name: 'course_name',
            targetKey: 'name',
            primaryKey: true,
        }
    })
    //#endregion

    //#region One-To-Many Contracts->Pupils
    db.Pupils.hasMany(db.Contracts, {
        foreignKey: {
            name: 'pupil_id',
            sourceKey: 'id',
            primaryKey: true,
        }
    })
    db.Contracts.belongsTo(db.Pupils, {
        foreignKey: {
            name: 'pupil_id',
            targetKey: 'id',
            primaryKey: true,
        }
    })
    //#endregion
    
    //#region One-To-Many Payrolls->Contracts
    db.Contracts.hasMany(db.Payrolls, {
        foreignKey: {
            name: 'contract_id',
            sourceKey: 'id',
            
        }
    })
    db.Payrolls.belongsTo(db.Contracts, {
        foreignKey: {
            name: 'contract_id',
            targetKey: 'id',
            
        }
    })
    //#endregion
    
    return db
}

Visualisation

if you need the project itself (its part of the database)

查看关于 hasMany 和 belongsTo 的 Sequelize v7 文档

https://sequelize.org/api/v7/classes/model#hasMany

https://sequelize.org/api/v7/classes/model#belongsTo

您传递给 foreignKey 对象的那些 sourceKey 和 targetKey 属性似乎应该是它们在选项对象中的属性,而不是嵌套在 foreignKey 中。

ContractsPayrolls 中的主键类型不兼容:
合同

        id: {
            type: DataTypes.STRING,
            allowNull: false,
            primaryKey: true,
            autoIncrement: false
        },

工资单

        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false,
            autoIncrement: true
        },

所以 DB 抛出了你没有捕捉到的错误。加.catch看看吧:

sequelize.sync({alter: false, force: true}).then(async () => {
    app.listen(PORT, () => {
        console.log(`http://localhost:${PORT}`);
    })
}).catch((err) => {
    console.error(err)
})

您还需要修复其他使用 Contacts 的关联,以从主键

中排除 id 以外的字段