Question

I'm currently reading the scala version of play's yabe tutorial. yabe stands for yet another blog engine and naturally at some point in the tutorial data needs to be stored. The first sql evolution is this one:

    # Users schema

# ---!Ups

CREATE TABLE User(
id bigint(20) NOT NULL AUTO_INCREMENT,
email varchar(255) NOT NULL,
password varchar(255) NOT NULL,
fullname varchar(255) NOT NULL,
isAdmin boolean NOT NULL,
PRIMARY KEY (id)
);

# --- !Downs

DROP TABLE User; 

After that tables for posts and comments are added. On the scala side each database entry can be mapped to a case class. Its companion object extends the trait Magic and implements various helper functions. The problem is caused by this code from the companion object of the Post class. You only need to look at the sql query:

def allWithAuthor:List[(Post,User)] = 
    SQL(
        """
            select * from Post p 
            join User u on p.author_id = u.id 
            order by p.postedAt desc
        """
    ).as( Post ~< User ^^ flatten * )

I admit that although I understand what the code does I would never have come up with this on my own.

In order to test the code the following tests are run:

it should "create a Post" in {

    User.create(User(Id(1), "bob@gmail.com", "secret", "Bob", false))     
    val users= User.find("id={id}").on("id"->1).as(User*)
}

This test finishes just fine. Scala's syntax adds some complexity but you can clearly see that the test queries for a user with id equals 1. The problem shows up in this test:

it should "retrieve Posts with author" in {

    User.create(User(Id(1), "bob@gmail.com", "secret", "Bob", false)) 
    Post.create(Post(NotAssigned, "My 1st post", "Hello world", new Date, 1))

    val posts = Post.allWithAuthor

    posts.length should be (1)

    val (post,author) = posts.head

    post.title should be ("My 1st post")
    author.fullname should be ("Bob")
}

The test fails with the error message:

ColumnNotFound(User.id) In /test/Tests.scala, line 41: val posts = Post.allWithAuthor

How can the column id disappear like that? I didn't change anything in the sql or scala code. Just swapping the tests "switches" the error on an off. Somehow this sql code

            select * from Post p 
            join User u on p.author_id = u.id 
            order by p.postedAt desc

doesn't find the id while this scala/sql code

val users= User.find("id={id}").on("id"->1).as(User*)

does.

Can you explain what went wrong ? Here's the link to the tutorial http://scala.playframework.org/documentation/scala-0.9.1/guide1

UPDATE:

I've read this question: ColumnNotFound problem with Magic in play scala

and following a comment edited the query. The sql itself hasn't changed but I've pasted it all in one single line:

def allWithAuthor:List[(Post,User)] = 
    SQL(
        """select * from Post p join User u on p.author_id = u.id order by p.postedAt desc"""
    ).as( Post ~< User ^^ flatten * )

It's a miracle: Now the column is found. If the query is longer than one line the test complains with the strange ColumnNotFoundError but with a oneliner everything's fine.

How can something like this happen ?

Was it helpful?

Solution

I guess your file is encoded with windows encoding. If you have windows encoding and your sql contains line break, there is this kind of problem.

Try with an UTF-8 encoded file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top