最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用MySQL的sequelize添加外键

IT培训 admin 13浏览 0评论

如何使用MySQL的sequelize添加外键

“我有2代表‘用户’和‘Profile_personals’我如何使用外键约束使用我添加到我的个人资料征婚启事‘user_ID的’主那是我的用户表里面?我和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
    }
);

如何使用MySQL的sequelize添加外键

“我有2代表‘用户’和‘Profile_personals’我如何使用外键约束使用我添加到我的个人资料征婚启事‘user_ID的’主那是我的用户表里面?我和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
    }
);
发布评论

评论列表 (0)

  1. 暂无评论