我正在与sequelize协会合作。我已经看到了许多添加外键的方法,这是我正确执行的方法吗?
我想做的是从父表(用户,“ 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
}
)
答案 0 :(得分:0)
您尝试过关联方法吗?
我认为HasMany
或HasOne
方法会起作用。
http://docs.sequelizejs.com/class/lib/associations/has-many.js~HasMany.html
user.hasMany(db.profile_personals, {foreignKey: 'user_id'})