[英]Sequelize create through association
我正在為兩個類之間的關聯開發一個create方法。 sequelize文檔表明這可以使用包括一步完成
IntramuralAthlete.create(intramuralAthlete,{
include: [Person]
}).then((data,err)=>{
if(data)res.json(data);
else res.status(422).json(err);
}).catch(function(error) {
res.status(422).json({message: "failed to create athlete", error: error.message});
});
我的模型關聯看起來像這樣
var Person = require('../models').person;
var IntramuralAthlete = require('../models').intramuralAthlete;
IntramuralAthlete.belongsTo(Person);
當我登錄時,校內運動員的價值是
{
person:
{ firstName: 'Test',
lastName: 'User',
email: 'test@user.com'
},
grade: '12th',
organizationId: 1
}
但我收到錯誤notNull Violation: personId cannot be null
。 這個錯誤讓我聽起來像Sequelize表示我打算在同一個調用中創建personId的方式聽起來有些不對勁。
在我向create
語句指示使用IntramuralAthlete創建的關聯表的方式中是否存在錯誤?
謝謝!
編輯:我也試過以下結構,結果相同
{
Person: {
firstName: 'Test',
lastName: 'User',
email: 'test@user.com'
},
grade: '12th',
organizationId: 1
}
我的模型如下:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('intramuralAthlete', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP')
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP')
},
grade: {
type: DataTypes.STRING,
allowNull: true
},
age: {
type: DataTypes.INTEGER(11),
allowNull: true
},
school: {
type: DataTypes.STRING,
allowNull: true
},
notes: {
type: DataTypes.STRING,
allowNull: true
},
guardianId: {
type: DataTypes.INTEGER(11),
allowNull: true,
references: {
model: 'contact',
key: 'id'
}
},
personId: {
type: DataTypes.INTEGER(11),
allowNull: false,
references: {
model: 'person',
key: 'id'
}
},
mobileAthleteId: {
type: DataTypes.INTEGER(11),
allowNull: true,
references: {
model: 'mobileAthlete',
key: 'id'
}
},
organizationId: {
type: DataTypes.INTEGER(11),
allowNull: false,
references: {
model: 'organization',
key: 'id'
}
}
}, {
tableName: 'intramuralAthlete'
});
};
我想你的模型名為Person
和IntramuralAthlete
( sequelize.define
方法的第一個參數)。 在這種情況下,當您創建類似於您的關聯,並且未定義as
屬性時,您的create
數據對象應如下所示
{
Person: {
firstName: 'Test',
lastName: 'User',
email: 'test@user.com'
},
grade: '12th',
organizationId: 1
}
如果你想使用person
(就像你的代碼一樣),你應該稍微改變一下這種關聯
IntramuralAthlete.belongsTo(Person, { as: 'person' });
然后,您必須在這樣的options
的include
屬性中對create
查詢執行一些更改
IntramuralAthlete.create(data, {
include: [
{ model: Person, as: 'person' }
]
}).then((result) => {
// both instances should be created now
});
編輯 :使用空值personId
欺騙save()
方法
如果你這樣做,你可以保持allowNull: false
{
person: {
// person data
},
personId: '', // whatever value - empty string, empty object etc.
grade: '12th',
organizationId: 1
}
編輯2 :創建時禁用驗證。
此案例假定驗證已關閉。 省略模型驗證似乎是一個壞主意,但是仍然維護數據庫表級驗證 - 在遷移中定義,它仍然可以檢查是否設置了personId
值
IntramuralAthlete.create(data, {
include: [
{ model: Person, as: 'person' }
],
validate: false
}).then((result) => {
// both instances should be created now
});
在這種情況下, data
對象可以在您的示例中 - 沒有personId
屬性。 我們省略了允許傳遞null
值的模型級驗證,但是如果在save()
方法期間它仍然是null
值 - 數據庫級驗證會引發錯誤。
首先,當您將模型與belongsTo關聯時,sequelize將自動將目標模型主鍵添加為源模型中的外鍵。 在大多數情況下,您不需要自己定義它,因此在您定義IntramuralAthlete.belongsTo(Person)
sequelize將PersonId
添加為IntramuralAthlete
的外鍵。 您的IntramuralAthlete
模型應如下所示:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('intramuralAthlete', {
grade: {
type: DataTypes.STRING,
allowNull: true
},
age: {
type: DataTypes.INTEGER(11),
allowNull: true
},
school: {
type: DataTypes.STRING,
allowNull: true
},
notes: {
type: DataTypes.STRING,
allowNull: true
}
});
};
現在您可以像上面的代碼一樣創建一個intramuralAthlete
。 例如:
let data = {
Person: {
firstName: 'Test',
lastName: 'User',
email: 'test@user.com'
},
grade: '12th',
notes: 'test notes'
}
IntramuralAthlete.create(data, {include: [Person]}).then((result) => {
// both instances should be created now
});
小心模型名稱。 第二,我想你的IntramuralAthlete
模型有多個belongsTo
關聯。 只需將它們定義為前一個關聯,sequelize將其主鍵作為IntramuralAthlete
模型中的外鍵添加。
第三,當你定義模型,sequelize會自動添加一個id
數據字段作為主鍵和自動增量,也增加了createdAt
和updatedAt
數據域有默認CURRENT_TIMESTAMP
值,所以你不需要在你的模型來定義它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.