Mysql Sequelize associate为一个模型创建userId和user_id,但不为另一个模型创建userId和user_id
“代理”和“代理”两种模式。每个都与我的“用户”模型相关联。我已经为每个模型指定了一个'user_id'的'foreignKey'。但是,Sequelize为一个模型而不是另一个模型在“user\u id”之外创建了一个“userId”,其中只创建了“user\u id” 编辑: 我的用户模型具有以下关联:Mysql Sequelize associate为一个模型创建userId和user_id,但不为另一个模型创建userId和user_id,mysql,node.js,sequelize.js,Mysql,Node.js,Sequelize.js,“代理”和“代理”两种模式。每个都与我的“用户”模型相关联。我已经为每个模型指定了一个'user_id'的'foreignKey'。但是,Sequelize为一个模型而不是另一个模型在“user\u id”之外创建了一个“userId”,其中只创建了“user\u id” 编辑: 我的用户模型具有以下关联: User.associate = (models) => { User.hasMany(models.agency) } User.associate = (mo
User.associate = (models) => {
User.hasMany(models.agency)
}
User.associate = (models) => {
User.hasMany(models.agent)
}
如何将其修复为没有“userId”
目前,以下表格是在MySQL中创建的
AGENCY
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| name | varchar(255) | YES | | NULL | |
| address1 | varchar(255) | YES | | NULL | |
| address2 | varchar(255) | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
| state | varchar(255) | YES | | NULL | |
| zipCode | varchar(255) | YES | | NULL | |
| country | varchar(255) | YES | | NULL | |
| license | varchar(255) | YES | | NULL | |
| createdAt | datetime | NO | | NULL | |
| updatedAt | datetime | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
AGENT
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| license | varchar(255) | YES | | NULL | |
| headline | varchar(255) | YES | | NULL | |
| createdAt | datetime | NO | | NULL | |
| updatedAt | datetime | NO | | NULL | |
| userId | int(11) | YES | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
我的代码如下:
// FILE agency.js
module.exports = (sequelize, type) => {
const Agency = sequelize.define('agency', {
id: {
type: type.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
user_id: {
type: type.INTEGER,
required: true
},
name: {
type: type.STRING
},
address1: {
type: type.STRING
},
address2: {
type: type.STRING
},
city: {
type: type.STRING
},
state: {
type: type.STRING
},
zipCode: {
type: type.STRING
},
country: {
type: type.STRING
},
license: {
type: type.STRING
}
},
{
freezeTableName: true,
})
Agency.associate = (models) => {
Agency.belongsTo(models.user, {
foreignKey: {
name: 'user_id'
}
})
}
return Agency
}
// FILE agent.js
module.exports = (sequelize, type) => {
const Agent = sequelize.define('agent', {
id: {
type: type.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
user_id: {
type: type.INTEGER,
required: true
},
license: {
type: type.STRING
},
headline: {
type: type.STRING
}
},
{
freezeTableName: true,
})
Agent.associate = (models) => {
Agent.belongsTo(models.user, {
foreignKey: {
name: 'user_id'
}
})
}
return Agent
}
// FILE db.js
const Sequelize = require('sequelize')
const config = require('../config').db
const db = {}
let sequelize = new Sequelize(config.database, config.username, config.password, config)
db.user = require('./user')(sequelize, Sequelize)
db.agency = require('./agency.js')(sequelize, Sequelize)
db.agent = require('./agent.js')(sequelize, Sequelize)
Object.keys(db).forEach(modelName => {
if(db[modelName].associate) {
db[modelName].associate(db)
}
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
您可能已经在用户模型中定义了:
User.hasOne(Agent)
// In this case hasOne will add an attribute userId to the Agent model!
根据您的代码更改此:
User.hasMany(models.agency);
User.hasMany(models.agent);
User.hasMany(models.agent,{foreignKey : 'user_id'});
User.hasMany(models.agency,{foreignKey : 'user_id'});
至:
User.hasMany(models.agency);
User.hasMany(models.agent);
User.hasMany(models.agent,{foreignKey : 'user_id'});
User.hasMany(models.agency,{foreignKey : 'user_id'});
原因:如果不定义foreignKey,它将创建userID
own
HasOne和BelongsTo之间的差异: HasOne和belong可在不同型号中插入关联密钥 从对方那里。HasOne在目标模型中插入关联键 而BelongsTo在源模型中插入关联键 当源模型中存在关联信息时,我们可以 使用belongsTo。在这种情况下,播放机适合于belongsTo,因为 它有teamId列
Player.belongsTo(Team) // `teamId` will be added on Player / Source model
当目标模型中存在关联信息时,我们可以
使用hasOne。在这种情况下,Coach适合hasOne,因为团队
模型将其Coach的信息存储为coachId字段
Coach.hasOne(Team) // `coachId` will be added on Team / Target model
请添加用户模型tooThanks Vivek,我的用户模型定义了以下关联:user.associate=(models)=>{user.hasMany(models.agency)}user.associate=(models)=>{user.hasMany(models.agent)}