RailsTutorial 3.2 Ch 11 - PostgreSQL 语法错误破坏了状态源

发布于 2025-01-08 06:05:10 字数 1870 浏览 0 评论 0原文

我位于 Rails 的 第 11.3.1 节教程,并且所有测试在此之前均已通过。之后,主页(具有微帖子提要)因以下错误而中断:

PG::Error: ERROR:  invalid input syntax for integer: "98, 1"
LINE 1: ...CT COUNT(*) FROM "microposts"  WHERE (user_id IN ('98, 1') O...
                                                             ^
: SELECT COUNT(*) FROM "microposts"  WHERE (user_id IN ('98, 1') OR user_id = 101)

并且一些测试因类似问题而失败。这是第一个:

1) Authentication authorization as wrong user visiting Users#edit page 
   Failure/Error: before { visit edit_user_path(wrong_user) }
   ActionView::Template::Error:
   PG::Error: ERROR:  invalid input syntax for integer: ""
   LINE 1: ...CT COUNT(*) FROM "microposts"  WHERE (user_id IN ('') OR use...
                                                                ^

现在,我使用的是PostgreSQL而不是默认的SQLite3,所以可能存在语法冲突,但我并不肯定。我对 Postgres 不太熟悉(只是用它来使 Heroku 部署更干净)。

看起来主页错误来自于用引号传递给查询的 ids - 我进入 psql 测试了一些查询,这是成功的:

SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" IN (1,2,3);

而这失败了:

SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" IN ('1,2,3');

规范错误来自于传递的空数组,相当于这个,这也失败了:

SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" IN ('');

熟悉 PostgreSQL 语法的人可以告诉我如何重写方法定义来解决这个问题吗?

micropost.rb 中的当前方法 看起来像这样:

def self.from_users_followed_by(user)
    followed_user_ids = user.followed_user_ids.join(', ')
    where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
end

来自的电话`users.rb' 看起来像这样:

def feed
    Micropost.from_users_followed_by(self)
end

I'm in Section 11.3.1 of the Rails Tutorial, and all tests were passing prior to this. Afterward, the home page (which has the micropost feed) breaks with this error:

PG::Error: ERROR:  invalid input syntax for integer: "98, 1"
LINE 1: ...CT COUNT(*) FROM "microposts"  WHERE (user_id IN ('98, 1') O...
                                                             ^
: SELECT COUNT(*) FROM "microposts"  WHERE (user_id IN ('98, 1') OR user_id = 101)

And several of the tests fail with a similar issue. Here's the first one:

1) Authentication authorization as wrong user visiting Users#edit page 
   Failure/Error: before { visit edit_user_path(wrong_user) }
   ActionView::Template::Error:
   PG::Error: ERROR:  invalid input syntax for integer: ""
   LINE 1: ...CT COUNT(*) FROM "microposts"  WHERE (user_id IN ('') OR use...
                                                                ^

Now, I am using PostgreSQL instead of the default SQLite3, so it may be that there is a syntax conflict, but I'm not positive. I'm not super familiar with Postgres (just using it to make the Heroku deploy cleaner).

It looks like the home page error is coming from the ids being passed to the query with quotes - I went into psql to test a few queries and this is successful:

SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" IN (1,2,3);

while this fails:

SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" IN ('1,2,3');

And the spec error is coming from an empty array being passed, equivalent to this, which also fails:

SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" IN ('');

Can anyone who is familiar with PostgreSQL syntax tell me how to rewrite the method definition to fix this issue?

The current method in micropost.rb looks like this:

def self.from_users_followed_by(user)
    followed_user_ids = user.followed_user_ids.join(', ')
    where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
end

And the call from `users.rb' looks like this:

def feed
    Micropost.from_users_followed_by(self)
end

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

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

发布评论

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

评论(1

鹿港巷口少年归 2025-01-15 06:05:10

天哪,我自己其实已经弄清楚了。只需删除方法定义中的连接:

def self.from_users_followed_by(user)
    followed_user_ids = user.followed_user_ids
    where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
end

user.followed_user_ids.join(', ') 生成:“1, 2, 3”,

user.followed_user_ids 生成这个: 1, 2, 3

这就是我想要的。

Holy crap, I actually figured this out myself. Just had to remove the join in the method definition:

def self.from_users_followed_by(user)
    followed_user_ids = user.followed_user_ids
    where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
end

user.followed_user_ids.join(', ') produces this: "1, 2, 3"

while

user.followed_user_ids produces this: 1, 2, 3

which is what I wanted.

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