[英]Foriegn key relation (NodeJS REST API with sequalize ORM)
我正在嘗試使用 NodeJS 創建 rest 和REST API 。 我使用MySql作為數據庫並使用sequelize ORM。
我試圖通過使用findOne連接兩個表來獲取數據。
contact
表與contact_email
表具有一對多的關系。
以下是這些表的續集 model:
聯系方式.model.js
const Contact = sequelize.define('contact', {
contact_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
unique: true,
primaryKey: true
},
user_id: {
type: DataTypes.INTEGER,
references: { model: User, key: 'user_id' }
},
first_name: {
type: DataTypes.STRING
},
last_name: {
type: DataTypes.STRING
},
nick_name: {
type: DataTypes.INTEGER,
},
dob: {
type: DataTypes.DATE
},
address: {
type: DataTypes.STRING,
},
city: {
type: DataTypes.STRING,
},
state: {
type: DataTypes.STRING,
},
country: {
type: DataTypes.STRING,
},
zip_code: {
type: DataTypes.STRING,
},
status: {
type: DataTypes.INTEGER,
defaultValue: 1
},
create_date: {
type: DataTypes.DATE
},
update_date: {
type: DataTypes.DATE
}
},
{
tableName: 'contact',
createdAt: 'create_date',
updatedAt: 'update_date'
}
);
const ContactEmail = sequelize.define('ContactEmail', {
contact_id: {
type: DataTypes.INTEGER,
primaryKey: true,
references: { model: Contact, key: 'contact_id' }
},
email: {
type: DataTypes.STRING,
primaryKey: true
}
},
{
tableName: 'contact_email',
timestamps: false
}
);
Contact.hasMany(ContactEmail, { foriegnKey: 'contact_id', as: "emails" })
ContactEmail.belongsTo(Contact)
module.exports = { Contact, ContactEmail, ContactPhone };
以下是獲取數據的續集 function:
聯系服務.js
const Contact = await ContactModel.Contact.findOne({
where: {
contact_id: contactId
},
include: [{
model: ContactModel.ContactEmail,
as: 'emails'
}]
})
當我調用 API 時,它會從數據中引發錯誤
“字段列表”中的未知列“emails.contactContactId”
自動生成的查詢如下:
SELECT `contact`.`contact_id`, `contact`.`user_id`, `contact`.`first_name`, `contact`.`last_name`, `contact`.`nick_name`, `contact`.`dob`, `contact`.`address`, `contact`.`city`, `contact`.`state`, `contact`.`country`, `contact`.`zip_code`, `contact`.`status`, `contact`.`create_date`, `contact`.`update_date`, `emails`.`contact_id` AS `emails.contact_id`, `emails`.`email` AS `emails.email`, `emails`.`contactContactId` AS `emails.contactContactId` FROM `contact` AS `contact` LEFT OUTER JOIN `contact_email` AS `emails` ON `contact`.`contact_id` = `emails`.`contactContactId` WHERE `contact`.`contact_id` = '5';
我的表沒有列contactContactId
。 它應該是contact_id
。 有誰知道這個的解決方案?
提前致謝。
// import the models Contact & ContactEmail
const contact = await Contact.findOne({
where: {
contact_id: contactId
},
include: [{
model: ContactEmail,
as: 'emails'
}]
})
const Contact = sequelize.define('contact', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
unique: true,
primaryKey: true
},
user_id: {
type: DataTypes.INTEGER,
references: { model: User, key: 'user_id' }
},
first_name: {
type: DataTypes.STRING
},
// ........
create_date: {
type: DataTypes.DATE
},
update_date: {
type: DataTypes.DATE
}
},
{
tableName: 'contact',
createdAt: 'create_date',
updatedAt: 'update_date'
}
);
const ContactEmail = sequelize.define('ContactEmail', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
},
contact_id: {
type: DataTypes.INTEGER,
references: { model: Contact, key: 'id' },
onUpdate: "CASCADE",
onDelete: "CASCADE"
},
email: {
type: DataTypes.STRING
}
},
{
tableName: 'contact_email',
timestamps: false
}
);
Contact.hasMany(ContactEmail, { foriegnKey: 'contact_id', as: "emails" })
ContactEmail.belongsTo(Contact)
module.exports = { Contact, ContactEmail, ContactPhone };
ContactEmail
應該具有與Contact
相同的foreignKey
選項:
Contact.hasMany(ContactEmail, { foriegnKey: 'contact_id', as: "emails" })
ContactEmail.belongsTo(Contact, , { foriegnKey: 'contact_id' })
您也可以查看我對類似問題的其他回答
通過如下修改模態已解決該問題。
聯系方式.model.js
const Contact = sequelize.define('contact', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
unique: true,
primaryKey: true,
field : 'contact_id'
},
user_id: {
type: DataTypes.INTEGER,
references: { model: User, key: 'user_id' }
},
first_name: {
type: DataTypes.STRING
},
// ........
create_date: {
type: DataTypes.DATE
},
update_date: {
type: DataTypes.DATE
}
},
{
tableName: 'contact',
createdAt: 'create_date',
updatedAt: 'update_date',
underscored : true
}
);
將field: 'contact_id'
添加到字段描述並更改名稱 ti id
並添加underscored: true
屬性。
謝謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.