sql 列“消失” - 游戏框架

发布于 2024-12-29 15:28:28 字数 2968 浏览 4 评论 0原文

我目前正在阅读 play 的 yabe 教程的 scala 版本。 yabe 代表另一个博客引擎,自然地在教程中的某个时刻需要存储数据。第一个 SQL 演变是这样的:

    # 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; 

之后添加了帖子和评论表。在 scala 方面,每个数据库条目都可以映射到一个案例类。它的伴生对象扩展了 Magic 特性并实现了各种辅助函数。该问题是由 Post 类的伴随对象中的这段代码引起的。你只需要查看 sql 查询:

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 should "create a Post" in {

    User.create(User(Id(1), "[email protected]", "secret", "Bob", false))     
    val users= User.find("id={id}").on("id"->1).as(User*)
}

该测试完成得很好。 Scala 的语法增加了一些复杂性,但您可以清楚地看到 id 等于 1 的用户的测试查询。 问题出现在这个测试中:

it should "retrieve Posts with author" in {

    User.create(User(Id(1), "[email protected]", "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")
}

测试失败并显示错误消息:

ColumnNotFound(User.id) 在 /test/Tests.scala 中,第 41 行:val posts = Post.allWithAuthor

列id怎么会这样消失呢?我没有更改 sql 或 scala 代码中的任何内容。只需交换测试即可“切换”错误。不知何故,这个 sql 代码

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

找不到 id,而这个 scala/sql 代码

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

却找到了。

你能解释一下出了什么问题吗? 这是教程的链接 http://scala.playframework.org/documentation/ scala-0.9.1/guide1

更新:

我读过这个问题: Magic in play scala 的 ColumnNotFound 问题

并按照评论编辑了查询。 sql 本身没有改变,但我将其全部粘贴到一行中:

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

这是一个奇迹:现在找到了该列。如果查询长于一行,则测试会抱怨奇怪的 ColumnNotFoundError 但使用 oneliner 一切都很好。

怎么会发生这样的事呢?

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), "[email protected]", "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), "[email protected]", "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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2025-01-05 15:28:28

我猜你的文件是用windows编码编码的。如果你有windows编码并且你的sql包含换行符,就会出现这种问题。

尝试使用 UTF-8 编码的文件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文