私はnode.jsにsequelizeを使用しています私はこの関係を定義します。モデルインスタンスの関連付けを変更して保存するにはどうすればよいですか?
// 1:M - Platform table with Governance status table
dbSchema.Platform_governance.belongsTo(dbSchema.Governance_status, {foreignKey: 'platform_status'});
dbSchema.Governance_status.hasMany(dbSchema.Platform_governance, {foreignKey: 'platform_status'});
だから、私はgovernance_status行を指す外部キーを持つplatform_governanceというテーブルがあることを意味します。ユーザーは、この外部キーを変更して、異なるgovernance_status行を指し示すことを望みます。
このように、まずユーザーが選択したgovernance_status行が実際に存在し、一意であることを検索してから、外部キーポイントを指定します。これは私が現在持っているものです。
// first I select the platform_governance of which I want to change it's foreign key
dbSchema.Platform_governance.findById(5, {include: [dbSchema.Governance_status]}).then(result => {
// Now I search for the user-requested governance_status
dbSchema.Governance_status.findAll({where:{user_input}}).then(answer => {
// I check that one and only one row was found:
if (answer.length != 1) {
console.log('error')
} else {
// Here I want to update the foreign key
// I want and need to do it through the associated model, not the foreign key name
result.set('governance_status', answer[0])
result.save().then(result => console.log(result.get({plain:true}))).catch(err => console.log(err))
}
result.save()約束を正常に戻し、コンソールに印刷されたオブジェクトが正しく設定新しいgovernance_statusで、正しいです。しかし、私がデータベースに行ったら、何も変わっていません。何も本当に救われませんでした。