gpt4 book ai didi

javascript - Sequelize belongsTo 关联给出错误 "table name "联系“指定不止一次”

转载 作者:行者123 更新时间:2023-12-03 22:19:29 24 4
gpt4 key购买 nike

我正在尝试连接 phone型号为contact模型通过contact_phone模型。当我.findAll如下所示,错误返回:未处理的拒绝 SequelizeDatabaseError:表名“联系人”指定了多次。在这种情况下,关联/“as”是否正确?当我尝试包含 contact_phone 时发生错误模型。我还插入了带有初始电话行的表格。

exports.findAll = (req, res) => {
models.contact.findAll({
include: [
{
model: models.contact_phone,
as: 'contact'
}
]
})
.then(contacts => {
res.status(200).send(contacts);
});
};

型号
module.exports = (sequelize) => {
const Contact = sequelize.define(
'contact',
{
contact_id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
field: 'contact_id'
},
first_name: {
type: Sequelize.STRING,
field: 'first_name'
},
last_name: {
type: Sequelize.STRING,
field: 'last_name',
},
created_at: {
type: Sequelize.DATE,
field: 'created_at'
},
updated_at: {
type: Sequelize.DATE,
field: 'updated_at'
}
},
{
tableName: 'contacts',
freezeTableName: true,
}
);

Contact.associate = (models) => {
Contact.belongsTo(models.contact_phone, {foreignKey: 'contact_id', as: 'contact'});
Contact.hasOne(models.phone, {foreignKey: 'phone_id', through: models.contact_phone});
};

return Contact;
};

module.exports = (sequelize) => {
const ContactPhone = sequelize.define(
'contact_phone',
{
contact_id: {
type: Sequelize.BIGINT,
references: {
model: 'contact',
key: 'contact_id'
}
},
phone_id: {
type: Sequelize.STRING,
references: {
model: 'phone',
key: 'phone_id'
}
}
}
);

ContactPhone.associate = (models) => {
ContactPhone.belongsTo(models.phone, {foreignKey: 'phone_id', as: 'phone'});
ContactPhone.belongsTo(models.contact, {foreignKey: 'contact_id', as: 'contact'});
};

return ContactPhone;
};

module.exports = (sequelize) => {
const Phone = sequelize.define(
'phone',
{
phone_id: {
type: Sequelize.STRING,
primaryKey: true,
field: 'phone_id'
},
updated_at: {
type: Sequelize.DATE,
field: 'updated_at'
},
created_at: {
type: Sequelize.DATE,
field: 'created_at'
}
},
{
tableName: 'phones',
freezeTableName: true,
}
);

Phone.associate = (models) => {
Phone.belongsTo(models.contact, {foreignKey: 'phone_id', through: models.contact_phone});
};

return Phone;
};

最佳答案

你可以这样做。您没有包含额外的contact_phone,您可以像这样直接使用联系人和电话进行关联。

联系模块:

module.exports = (sequelize) => {
const Contact = sequelize.define(
'contact',
{
contact_id: {
type: Sequelize.INT,
primaryKey: true,
autoIncrement: true,
field: 'contact_id'
},
first_name: {
type: Sequelize.STRING,
field: 'first_name'
},
last_name: {
type: Sequelize.STRING,
field: 'last_name',
},
created_at: {
type: Sequelize.DATE,
field: 'created_at'
},
updated_at: {
type: Sequelize.DATE,
field: 'updated_at'
}
},
{
tableName: 'contacts',
freezeTableName: true,
}
);

Contact.associate = (models) => {
Contact.hasOne(models.phone, { // has many or hasOne relationship .here read like contact has one phone
foreignKey: 'phone_id',
onDelete: "CASCADE"
});
};

return Contact;
};

手机模块:
module.exports = (sequelize) => {
const Phone = sequelize.define(
'phone',
{
phone_id: {
type: Sequelize.INT,
primaryKey: true,
autoIncrement: true,
field: 'phone_id'
},
contact_id: {
type: Sequelize.INT,
},
updated_at: {
type: Sequelize.DATE,
field: 'updated_at'
},
created_at: {
type: Sequelize.DATE,
field: 'created_at'
}
},
{
tableName: 'phones',
freezeTableName: true,
}
);

Phone.associate = (models) => {
Phone.belongsTo(models.contact, {
foreignKey: 'contact_id',
as: 'contact',
onDelete: "CASCADE"
});
};
}

您的查询类似于:
models.contact.findAll({
include: [
{
model: models.phone,
as: 'contact'
}
]
}).then(contacts => {
res.status(200).send(contacts);
});

编辑:

如何定义更多与相同键和不同别名的关联
Phone.associate = (models) => {
Phone.belongsTo(models.contact, {
foreignKey: 'contact_id',
as: 'contact',
onDelete: "CASCADE"
});
Phone.belongsTo(models.contact, {
foreignKey: 'contact_id',
as: 'phone',
onDelete: "CASCADE"
});
};

关于javascript - Sequelize belongsTo 关联给出错误 "table name "联系“指定不止一次”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62318368/

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