使用“结束”作为 Ruby on Rails (MySQL) 中的列名称

发布于 2024-10-30 23:31:30 字数 297 浏览 1 评论 0原文

我有一个带有“结束”列(日期时间格式)的模型,却发现每当我尝试在查询中引用该列时,Heroku 都会崩溃并因不合逻辑的 Active Record 错误而烧毁。我花了两个小时尝试调试这个极其简单的查询,之后我将该列重命名为“end_at”,所有问题都消失了。

还有其他人遇到过这个问题吗?我很好奇这背后的原因,并希望我们可以帮助其他人避免同样的错误。 之前曾提出过类似的问题,但没有给出明确的答案。

I had an model with an "end" column (datetime format), only to discover that Heroku crashes and burns with illogical Active Record errors whenever I attempted to reference the column in a query. I spent two hours trying to debug the extremely simple query, after which point I renamed the column to "end_at" and all of my problems disappeared.

Has anybody else experienced this issue? I'm curious of the reasoning behind this and hope that we can help others avoid the same mistake. A similar question has been asked before, but a clear answer was not presented.

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

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

发布评论

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

评论(2

影子的影子 2024-11-06 23:31:30

BEGIN和END是Oracle和SQL Server中的保留字,但不知道为什么MySQL 不认为它们是这样的

然而,PGError 似乎表明数据库引擎本身(而不是任何与 Ruby 相关的运行时)确实因“结束”而拒绝了查询。

如果引用,则可以使用保留字(以及包含空格的名称) - 也许 Active Record 没有引用生成的 SQL 中的标识符。

我会查看 MySQL 中的日志(http://dev.mysql.com/doc/refman/5.5/en/query-log.html)并查看生成的语句。

由于 PGError 意味着 PostGreSQL 并且您提到了 Heroku (PostgreSQL 8.3) - 我认为这是因为 END 确实是 PostgreSQL 中的保留字: http://www.postgresql.org/docs/8.3/static/sql-keywords-appendix.html

http://www.petefreitag.com/tools/sql_reserved_words_checker/?word=end

BEGIN and END are reserved words in Oracle and SQL Server, but not sure why MySQL doesn't consider them as such.

However that PGError would appear to indicate that the database engine itself (and not any Ruby-related runtime) has indeed rejected the query because of the "end".

Reserved words (and names containing spaces) can be used if quoted - perhaps Active Record didn't quote the identifiers in the SQL which was generated.

I would look at the log in MySQL (http://dev.mysql.com/doc/refman/5.5/en/query-log.html) and see the statements generated.

And since the PGError means PostGreSQL and you mentioned Heroku (PostgreSQL 8.3) - I think this is because END is indeed a reserved word in PostgreSQL: http://www.postgresql.org/docs/8.3/static/sql-keywords-appendix.html

http://www.petefreitag.com/tools/sql_reserved_words_checker/?word=end

忆梦 2024-11-06 23:31:30

PostgresQL(Heroku 使用)保留 END 作为关键字 ,所以它会给你一个语法错误,因为你的语法不正确。

有两个选项可以修复它:

如果 Heroku 由于 ActiveRecord 未引用列名而损坏,您可以重写使用该模型的每个查询以显式引用“结束”列,这样 PostgresQL 就不会崩溃。

无论 ActiveRecord 默认情况下是否引用该列名,都将该列重命名为更具描述性的名称(end_time、end_date 等),并且不要使用您编写应用程序的语言中的保留字以及 SQL 引擎中的保留字你正在使用。

PostgresQL (which Heroku uses) reserves END as a keyword, so it is giving you a syntax error because your syntax is incorrect.

There are two options for fixing it:

If Heroku is breaking because ActiveRecord is not quoting column names, you can rewrite every query that uses that model to explicitly quote the "end" column so PostgresQL doesn't blow up.

Whether or not ActiveRecord quotes that column name by default, renaming the column something more descriptive (end_time, end_date, etc) and also not a reserved word in both the language you are writing the app in as well as a reserved word in the SQL engine you are using.

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