【问题标题】:Strange result when using squeryl and scala使用 squeryl 和 scala 时出现奇怪的结果
【发布时间】:2013-02-28 22:07:10
【问题描述】:

我正在尝试通过获取正确的linkedAccount 来选择耦合用户。 创建的查询是正确的,但是在尝试使用属性时 在 dbuser 例如 dbuser.lastName 我得到一个编译错误,因为 dbuser 不是 类型为 User 但 Query1 大小=?
这可能很简单,但我无法弄清楚,因为我是 一个 scala 和 squeryl 菜鸟!

为什么它不返回正确的值,我在查询中做错了什么? 此外,保存工作没有任何问题。

用户:

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)

}

应用数据库:

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'))
  ) ))

【问题讨论】:

    标签: scala one-to-many squeryl


    【解决方案1】:

    顺便说一句,在@Column@ColumnBase 的文档中说:

    定义列元数据的首选方法是不定义它们(!)

    因此,您可以将列定义为

    val id: Long,

    而不是

    @Column("id") val id: Long

    【讨论】:

    • 啊哈!感谢您的提示!
    【解决方案2】:

    好的,想通了。在这种情况下,我需要拨打电话:

    .headOption
    

    在 Per 的一些提示之后也修复了查询

    def userByLinkedAccount(providerKey : String, providerUserId : String) = {
        inTransaction {
          from(AppDB.users, AppDB.linkedAccounts)((u,la) =>
            where (u.id === la.userId and la.providerKey === providerKey and la.providerUserId === providerUserId)
              select(u)
          ).headOption
        }
      }
    

    【讨论】:

    • 您可能需要考虑改用headOption,在您的情况下它将返回Option[User]。小心使用single,因为如果没有数据(或返回多于一行)会抛出异常。
    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 2013-03-10
    • 2018-06-27
    • 2019-09-22
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多