2016-05-04 32 views
1

sails/waterline文檔中的一對多示例代碼假定主鍵是兩個模型之間的關聯(我認爲)。在不使用主鍵的情況下填充js中的一對多關係

http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-to-many

但是我有有一個轉診列引用類似的一些其他值

User 
{ 
    id (primary): <int> 
    email: <string> 
} 

recordings 
{ 
    id (primary): <int> 
    email: <that email from user> 
} 

ATM我嘗試

userModel.js 
{ 
    attributes: { 
     email: { 
     type: 'string', 
      size: 255, 
      unique: true 
     }, 
     }, 
     queries: { 
     columnName: 'email', 
     model: 'recordings' 
     } 
     ..... 
    } 
} 

recordingsModel.js 
{ 
    attributes: { 
     email: { 
     type: 'string', 
      size: 255, 
     }, 
     }, 
     queries: { 
     model: 'user' 
     } 
     ..... 
    } 
} 

,並在控制器

sails.models.user 
    .find() 
    .populate('queries') 
    .exec(function (err, results) { 

}); 
模型

但我得到的錯誤 :ER_BAD_FIELD_ERROR:在「字段列表」未知列「__queries.queries」

沒有人有在水線許多關係良好的教程之一,因爲在那裏的網站上的文檔是非常差這麼我覺得我只是不理解如何設計模型。

+0

在recordingsModel中你有屬性''查詢'''而且列不存在。嘗試刪除它。 – Bonanza

回答

1

據我所知,你想要的是能夠建立你的userModelrecordingsModel模型,例如,通過賦予了email記錄一定值時,它會自動與共享電子郵件的任何用戶相關聯。這不是水線(帆船ORM)所支持的。

相反,你最好的選擇是設置型號多達如文檔中表示:

// userModel.js 
{ 
    attributes: { 
     email: { 
     type: 'string', 
      size: 255, 
      unique: true 
     }, 
     }, 
     recordings: { 
     collection: 'recordingsModel', 
     via: 'user' 
     } 
     ..... 
    } 
} 

// recordingsModel.js 
{ 
    attributes: { 
     email: { 
     type: 'string', 
      size: 255, 
     }, 
     }, 
     user: { 
     model: 'userModel' 
     } 
    } 
} 

我的猜測是,你要避免查找用戶ID,以關聯起來新的錄音。如果您製作userModel的主鍵email,則可以執行此操作;那麼當你創建一個新的錄音時,你可以將其user設置爲一個電子郵件地址,並且這兩個鏈接。

+0

不幸的是,我在一個預先存在的數據庫之上添加風帆我不會改變,因爲其他系統依賴它。不幸的是,帆似乎有點更加自以爲是,然後我希望。因爲這個原因,可能會放棄傳統mysql連接的水線。 – Neablis

+0

您可以隨時使用['.query'](http://sailsjs.org/documentation/reference/waterline-orm/models/query)從Waterline進行自定義SQL查詢。 – sgress454

+1

雅,但那麼使用吃水線怎麼樣?不幸的是,我爲每個終端都做了越來越多的工作。 – Neablis

相關問題