RailsTutorial 3.2 Ch 11 - PostgreSQL 语法错误破坏了状态源
我位于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
天哪,我自己其实已经弄清楚了。只需删除方法定义中的连接:
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:
user.followed_user_ids.join(', ')
produces this: "1, 2, 3"while
user.followed_user_ids
produces this: 1, 2, 3which is what I wanted.