Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

includes + hasMany doesn't work with non-default primaryKeyFieldName #347

Closed
seratch opened this issue Feb 1, 2017 · 0 comments
Closed

Comments

@seratch
Copy link
Member

seratch commented Feb 1, 2017

class Issue346Spec extends fixture.FunSpec with Matchers
    with Connection
    with CreateTables
    with AutoRollback {

  case class User(userId: Long, name: String, createdAt: DateTime, articles: Seq[Article] = Nil)
  case class Article(id: Long, title: String, userId: Option[Long], user: Option[User] = None)

  object User extends SkinnyCRUDMapper[User] {
    override val connectionPoolName = 'issue346
    override val primaryKeyFieldName = "userId"
    override def defaultAlias = createAlias("u")

    lazy val articlesRef = hasMany[Article](
      many = Article -> Article.defaultAlias,
      on = (u, a) => sqls.eq(u.userId, a.userId),
      merge = (u, as) => u.copy(articles = as)
    ).includes[Article](
      merge = {
      (users, articles) =>
        users.map { user => user.copy(articles = articles.filter(_.userId.exists(_ == user.userId))) }
    }
    )

    override def extract(rs: WrappedResultSet, rn: ResultName[User]) = autoConstruct(rs, rn, "articles")
  }

  object Article extends SkinnyCRUDMapper[Article] {
    override val connectionPoolName = 'issue346
    override def defaultAlias = createAlias("a")
    override def extract(rs: WrappedResultSet, rn: ResultName[Article]) = autoConstruct(rs, rn, "user")

    lazy val userRef = belongsTo[User](
      right = User,
      merge = (a, u) => a.copy(user = u)
    )
  }

  override def db(): DB = NamedDB('issue346).toDB()

  override def fixture(implicit session: DBSession): Unit = {
    val aliceId = User.createWithAttributes('name -> "Alice")
    val bobId = User.createWithAttributes('name -> "Bob") // Scala
    val chrisId = User.createWithAttributes('name -> "Chris") // Scala
    val denId = User.createWithAttributes('name -> "Den")
    val ericId = User.createWithAttributes('name -> "Eric") // Scala
    val fredId = User.createWithAttributes('name -> "Fred")

    val titleAndUser = Seq(
      ("Hello World", Some(aliceId)),
      ("Getting Started with Scala", Some(bobId)), // Scala
      ("Functional Programming in Scala", None), // Scala
      ("Beginning Ruby", Some(aliceId)),
      ("Beginning Scala", Some(chrisId)), // Scala
      ("Beginning Ruby", Some(denId)),
      ("Hello Scala", Some(ericId)), // Scala
      ("Bob's Scala Lesson 1", Some(bobId)), // Scala
      ("Functional Programming in Java", Some(fredId)),
      ("Beginning Ruby", Some(fredId)),
      ("Scalaz Usage", Some(chrisId)), // Scala
      ("The Better Java?", Some(bobId)),
      ("How to user sbt", Some(aliceId))
    )
    titleAndUser.foreach {
      case (title, userId) =>
        Article.createWithAttributes('title -> title, 'userId -> userId)
    }
  }

  describe("joins/includes") {

    it("should return expected results when joins / includes") { implicit session =>
      val users1 = User.joins(User.articlesRef).findAll()
      val users2 = User.includes(User.articlesRef).findAll()
      users1 should equal(users2)
    }
  }

}

test failure

[info] - should return expected results when joins / includes *** FAILED ***
[info]   List(User(1,Alice,2017-02-01T22:25:18.822+09:00,Vector(Article(1,Hello World,Some(1),None), Article(4,Beginning Ruby,Some(1),None), Article(13,How to user sbt,Some(1),None))), User(2,Bob,2017-02-01T22:25:18.822+09:00,Vector(Article(2,Getting Started with Scala,Some(2),None), Article(8,Bob's Scala Lesson 1,Some(2),None), Article(12,The Better Java?,Some(2),None))), User(3,Chris,2017-02-01T22:25:18.822+09:00,Vector(Article(5,Beginning Scala,Some(3),None), Article(11,Scalaz Usage,Some(3),None))), User(4,Den,2017-02-01T22:25:18.822+09:00,Vector(Article(6,Beginning Ruby,Some(4),None))), User(5,Eric,2017-02-01T22:25:18.822+09:00,Vector(Article(7,Hello Scala,Some(5),None))), User(6,Fred,2017-02-01T22:25:18.822+09:00,Vector(Article(9,Functional Programming in Java,Some(6),None), Article(10,Beginning Ruby,Some(6),None)))) did not equal List(User(1,Alice,2017-02-01T22:25:18.822+09:00,List(Article(1,Hello World,Some(1),None), Article(4,Beginning Ruby,Some(1),None))), User(2,Bob,2017-02-01T22:25:18.822+09:00,List(Article(2,Getting Started with Scala,Some(2),None))), User(3,Chris,2017-02-01T22:25:18.822+09:00,List(Article(5,Beginning Scala,Some(3),None))), User(4,Den,2017-02-01T22:25:18.822+09:00,List(Article(6,Beginning Ruby,Some(4),None))), User(5,Eric,2017-02-01T22:25:18.822+09:00,List()), User(6,Fred,2017-02-01T22:25:18.822+09:00,List())) (Issue346Spec.scala:116)

joins

List(User(1,Alice,2017-02-01T22:25:18.822+09:00,Vector(Article(1,Hello World,Some(1),None), Article(4,Beginning Ruby,Some(1),None), Article(13,How to user sbt,Some(1),None))), User(2,Bob,2017-02-01T22:25:18.822+09:00,Vector(Article(2,Getting Started with Scala,Some(2),None), Article(8,Bob's Scala Lesson 1,Some(2),None), Article(12,The Better Java?,Some(2),None))), User(3,Chris,2017-02-01T22:25:18.822+09:00,Vector(Article(5,Beginning Scala,Some(3),None), Article(11,Scalaz Usage,Some(3),None))), User(4,Den,2017-02-01T22:25:18.822+09:00,Vector(Article(6,Beginning Ruby,Some(4),None))), User(5,Eric,2017-02-01T22:25:18.822+09:00,Vector(Article(7,Hello Scala,Some(5),None))), User(6,Fred,2017-02-01T22:25:18.822+09:00,Vector(Article(9,Functional Programming in Java,Some(6),None), Article(10,Beginning Ruby,Some(6),None))))

includes

List(User(1,Alice,2017-02-01T22:25:18.822+09:00,List(Article(1,Hello World,Some(1),None), Article(4,Beginning Ruby,Some(1),None))), User(2,Bob,2017-02-01T22:25:18.822+09:00,List(Article(2,Getting Started with Scala,Some(2),None))), User(3,Chris,2017-02-01T22:25:18.822+09:00,List(Article(5,Beginning Scala,Some(3),None))), User(4,Den,2017-02-01T22:25:18.822+09:00,List(Article(6,Beginning Ruby,Some(4),None))), User(5,Eric,2017-02-01T22:25:18.822+09:00,List()), User(6,Fred,2017-02-01T22:25:18.822+09:00,List()))
seratch added a commit to seratch/skinny-framework that referenced this issue Feb 1, 2017
seratch added a commit that referenced this issue Feb 1, 2017
Fix #347 includes + hasMany doesn't work with non-default primaryKeyFieldName
seratch added a commit that referenced this issue Feb 1, 2017
 - [blank-app] Move to scalate/sbt-scalate-precompiler by @seratch
 - [blank-app] #346 Fix procedure syntax in Bootstrap class by @xuwei-k
 - [orm] #348 Fix #347 includes + hasMany doesn't work with non-default primaryKeyFieldName by @seratch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant