[英]Sequelize - Association Many to Many - 3 Foreign Keys
我正在尋找有關在 Sequelize 模型中正確定義具有 3 個外鍵的關聯表的一些幫助:
情況:我正在構建一個電子郵件客戶端,這個問題的相關模型是:
問題:我的問題是關聯模型/表 (ThreadFolder),我無法為 ThreadFolder 關聯模型中的所有 3 個表創建關聯。
第一次嘗試我能夠創建與 Sequelize 的關聯,允許 ThreadFolder 模型為上述三個模型中的兩個創建外鍵,但不是全部 3 個。這是關聯:
Thread.belongsToMany(Folder, { through: ThreadFolder, foreignKey: 'thread_id', otherKey: 'folder_id' })
Folder.belongsToMany(Thread, { through: ThreadFolder, foreignKey: 'folder_id', otherKey: 'thread_id' })
SQL 輸入嘗試:
第一次嘗試的錯誤:
Executing:
INSERT INTO `iwantmail-core`.`thread_folders` (`user_id`, `deleted`, `archived`, `created_at`, `updated_at`, `thread_id`, `folder_id`) VALUES ('1', '0', '0', '2020-03-05 23:34:16', '2020-03-05 23:34:16', '30', '1');
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1062: 1062: Duplicate entry '30-1' for key 'PRIMARY'
SQL Statement:
INSERT INTO `iwantmail-core`.`thread_folders` (`user_id`, `deleted`, `archived`, `created_at`, `updated_at`, `thread_id`, `folder_id`) VALUES ('1', '0', '0', '2020-03-05 23:34:16', '2020-03-05 23:34:16', '30', '1')
第二次嘗試我可以指定如下所示的關聯,以允許我添加具有不同 user_id 和 folder_id 的記錄,但是,如果我使用不同的 thread_id,我會收到如下所示的錯誤。
Folder.hasMany(ThreadFolder, { foreignKey: 'folder_id' })
ThreadFolder.belongsTo(Folder, { foreignKey: 'folder_id' })
Thread.belongsToMany(Folder, { through: ThreadFolder })
Thread.hasMany(ThreadFolder, { foreignKey: 'thread_id' })
ThreadFolder.belongsTo(Thread, { foreignKey: 'thread_id' })
User.hasMany(ThreadFolder, { foreignKey: 'user_id' })
ThreadFolder.belongsTo(User, { foreignKey: 'user_id' })
Folder.belongsToMany(User, { through: ThreadFolder })
SQL 輸入嘗試:
第二次嘗試的錯誤
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1062: 1062: Duplicate entry '1-1' for key 'PRIMARY'
SQL Statement:
INSERT INTO `mail-core`.`thread_folders` (`user_id`, `thread_id`, `folder_id`) VALUES ('1', '2', '1')
請注意,我基本上是想指出 Thread#1 的 User#1 位於 Folder#1 中,一旦我嘗試指出 Thread#2 的 User#1 位於 Folder#1 中,就會發生上述錯誤。
幫助:
提前感謝您的任何幫助/協助!
(使用的相關技術:MySQL、MySQL Workbench、Node 12.x、TypeScript、Serverless Framework)
編輯:對帖子進行了編輯,第二次嘗試作為部分解決方案提出,經過進一步測試,當第二個用戶添加到關聯表 ThreadFolder 中的同一線程和文件夾時,第一次和第二次嘗試均失敗。
查看您的創建語句后,我認為您已經正確定義了關聯thread_folders
。 在我看來,您第二次嘗試定義關聯是正確的。
插入記錄時出現錯誤,因為您的主鍵是兩個屬性的組合鍵,即thread_id
和folder_id
。 假設在您的thread_folders
表中已經有thread_id
1 和folder_id
1 的記錄,那么您不能插入另一個具有thread_id
1 和folder_id
1 的記錄。
如果您刪除thread_id
和folder_id
的組合主鍵,那么您將能夠在thread_folders
表中插入要插入的記錄。
我希望它有幫助!
工作解決方案最終成為第二次嘗試的差異:
User.hasMany(ThreadFolder, { foreignKey: 'user_id' })
ThreadFolder.belongsTo(User, { foreignKey: 'user_id' })
Folder.hasMany(ThreadFolder, { foreignKey: 'folder_id' })
ThreadFolder.belongsTo(Folder, { foreignKey: 'folder_id' })
Thread.hasMany(ThreadFolder, { foreignKey: 'thread_id' })
ThreadFolder.belongsTo(Thread, { foreignKey: 'thread_id' })
我的大部分模型調用(基於它們之前的編寫方式)最終都會更改為首先從關聯表開始,例如
ThreadFolder.findAll({
where: {
user_id: 1,
folder_id: 1,
},
include: [
{
model: Thread,
include: [
'recipient',
'sender'
]
}
]
})
希望這有助於其他試圖做超過 2 個外鍵的多外鍵關聯表的人,假設這種方法應該適用於關聯表中的任意數量的外鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.