如何使用sequelize mysql添加外键

如何使用sequelize mysql添加外键,mysql,node.js,sequelize.js,Mysql,Node.js,Sequelize.js,“我有两个表“Users”和“Profile_personals”。如何使用我的“Users_id”主表(位于我的Users表中)向我的Profile personals添加外键约束?我正在使用node.js,sequelize mysql Users(parent Table): const Sequelize = require('sequelize') const db = require("../database/db.js") module.exports

“我有两个表“Users”和“Profile_personals”。如何使用我的“Users_id”主表(位于我的Users表中)向我的Profile personals添加外键约束?我正在使用node.js,sequelize mysql

Users(parent Table):

    const Sequelize = require('sequelize')
    const db = require("../database/db.js")

    module.exports = db.sequelize.define(
        "users", 
        {
            user_id: {
                type: Sequelize.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            email: {
                type: Sequelize.STRING
            },
            password: {
                type: Sequelize.STRING
            }
        },    
        {
            timestamps: false
        }
    )


    Personals(Child Table):


    const Sequelize = require('sequelize')
    const db = require("../database/db.js")

    module.exports = db.sequelize.define(
        'profile_personals', 
        {
            id: {
                type: Sequelize.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            biography: {
                type: Sequelize.STRING
            }       
        },    
        {
            timestamps: false
        }
    )

这样做吧,我希望这就是你想要的

module.exports = db.sequelize.define(
    'profile_personals',
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        biography: {
            type: Sequelize.STRING
        },
        // It is possible to create foreign keys:
        user_id: {
            type: Sequelize.INTEGER,

            references: {
                // This is a reference to another model
                model: Users,

                // This is the column name of the referenced model
                key: 'user_id'
            }
        }
    },
    {
        timestamps: false
    }
);

这样做吧,我希望这就是你想要的

module.exports = db.sequelize.define(
    'profile_personals',
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        biography: {
            type: Sequelize.STRING
        },
        // It is possible to create foreign keys:
        user_id: {
            type: Sequelize.INTEGER,

            references: {
                // This is a reference to another model
                model: Users,

                // This is the column name of the referenced model
                key: 'user_id'
            }
        }
    },
    {
        timestamps: false
    }
);

谢谢@ccordon的响应,因此此代码将自动添加外键并与用户id匹配?我尝试了它,并按照您所说的那样工作,但我的问题是,此外键如何与用户id连接并提取与特定用户相关的数据?您想获得具有相同用户id的所有个人资料吗?这就是我理解你的意思。是的,正确的,谢谢你的回答。它解决了我的问题。谢谢@ccordon的响应,因此此代码将自动添加外键并与用户id匹配?我尝试了它,它按照你所说的工作,但我的问题是,此外键如何与用户id连接并提取与特定用户相关的数据ar user I您想获得所有具有相同用户id的个人资料吗?这就是我了解您的原因。是的,正确,谢谢您的回答。它解决了我的问题。