gpt4 book ai didi

mysql - 使用 Sequelize 添加外键约束

转载 作者:行者123 更新时间:2023-11-29 16:14:07 24 4
gpt4 key购买 nike

我正在与 Sequelize 协会合作。我见过很多添加外键的方法,我的做法正确吗?

我想做的是从我的父表(users,“user_id”)向我的 profile_personals 表添加外键约束

const Sequelize = require('sequelize')
const db = require('../database/db.js')
const Users = require('../model/User.js')

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

biography: {
type: Sequelize.STRING
},
fk_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
}
)

最佳答案

你尝试过关联方法吗?我认为 HasManyHasOne 方法会起作用。

http://docs.sequelizejs.com/class/lib/associations/has-many.js~HasMany.html

user.hasMany(db.profile_personals, {foreignKey: 'user_id'})

关于mysql - 使用 Sequelize 添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com