我想通過獲取正確的linkedAccount來選擇耦合用戶。 創建的查詢是正確的,但試圖在dbuser上使用屬性 例如dbuser.lastName
我得到編譯錯誤,因爲dbuser不是 類型爲User但Query1大小=?
它可能是一個非常簡單的東西,但我無法弄清楚,因爲我是 scala和squeryl noob!使用squeryl和scala時出現奇怪的結果
爲什麼它不返回正確的值,以及我在查詢中做了什麼錯誤? 另外,保存作品沒有任何問題。
用戶:
class User(
@Column("id") val id: Long,
@Column("first_name") val firstName : String,
@Column("last_name") val lastName : String,
@Column("email") val email : String,
@Column("email_validated") val emailValidated: Boolean = false,
@Column("last_login") val lastLogin: Timestamp = null,
val created: Timestamp,
val modified: Timestamp,
val active: Boolean = false
) extends KeyedEntity[Long] {
lazy val linkedAccounts: OneToMany[LinkedAccount] = AppDB.usersToLinkedAccounts.left(this)
}
LINKEDACCOUNT:
class LinkedAccount(
@Column("id") val id: Long,
@Column("user_id") val userId: Long,
@Column("provider_user_id") val providerUserId: String,
@Column("salt") val salt: String,
@Column("provider_key") val providerKey: String) extends KeyedEntity[Long] {
lazy val user: ManyToOne[User] = AppDB.usersToLinkedAccounts.right(this)
}
AppDB:
object AppDB extends Schema {
val users = table[User]("users")
val linkedAccounts = table[LinkedAccount]("linked_account")
val usersToLinkedAccounts = oneToManyRelation(users, linkedAccounts).via((u, l) => u.id === l.userId)
def userByLinkedAccount(prodivderKey: String, providerUserId: String) = {
from(AppDB.users)(u =>
where(u.id in
from(AppDB.linkedAccounts)(la =>
where(la.userId === u.id and la.providerKey === prodivderKey and la.providerUserId === providerUserId)
select (la.userId)
)
)
select (u)
)
}
呼叫:
val dbuser = inTransaction {
val u2 = AppDB.userByLinkedAccount(id.providerId, id.id)
println(u2.statement)
}
println(dbuser.lastName)
生成的SQL
Select
users10.last_login as users10_last_login,
users10.email as users10_email,
users10.modified as users10_modified,
users10.last_name as users10_last_name,
users10.first_name as users10_first_name,
users10.id as users10_id,
users10.created as users10_created,
users10.email_validated as users10_email_validated,
users10.active as users10_active
From
users users10
Where
(users10.id in ((Select
linked_account13.user_id as linked_account13_user_id
From
linked_account linked_account13
Where
(((linked_account13.user_id = users10.id) and (linked_account13.provider_key = 'facebook')) and (linked_account13.provider_user_id = 'XXXXXXXXXX'))
)))
你可能要考慮使用'headOption'代替,而你的情況會返回一個'選項[用戶]'。小心使用'single',因爲如果沒有數據(或者返回多行)它會引發異常。 – jcern 2013-02-28 13:14:44
啊!完美的提示!啊哈! – jakob 2013-02-28 14:06:31