【问题标题】:PG syntax errorPG 语法错误
【发布时间】:2013-06-14 07:02:29
【问题描述】:

我一直在尝试将我学校基于 ruby​​ on rails 构建的 fedena project 从 mySQL 转换为 postgreSQL 以进行 heroku 部署,但遇到以下错误:

显示第 40 行出现的 app/views/class_timings/index.html.erb:

PG::Error: ERROR:  syntax error at or near "."
LINE 1: SELECT `batches`.*,CONCAT(courses.code,'-',batches.name) as ...
                        ^

: SELECT `batches`.*,CONCAT(courses.code,'-',batches.name) as course_full_name FROM "batches"   INNER JOIN "courses" ON "courses".id = "batches".course_id  WHERE ("batches"."is_deleted" = 'f' AND "batches"."is_active" = 't')  ORDER BY course_full_name

提取的源代码(第 40 行附近):

37:     <label ><%= t('select_a_batch') %>:</label>
38:     <div class="text-input-bg">
39:       <%= select :batch, :id,
40:         @batches.map {|b| [b.full_name, b.id] },
41:         {:prompt => "#{t('common')}"},
42:         {:onchange => "#{remote_function(
43:         :url => { :action => 'show' },
44:         :with => "'batch_id='+value",
45:         :before => "Element.show('loader')",
46:         :success => "Element.hide('loader')"
47:         )}"} %>

显然,我是编程新手!请帮忙。

控制器:

class_timings_controller.rb

  def index
    @batches = Batch.active
    @class_timings = ClassTiming.find(:all,:conditions => { :batch_id => nil,:is_deleted=>false}, :order =>'start_time ASC')
  end

【问题讨论】:

    标签: ruby-on-rails postgresql syntax-error


    【解决方案1】:

    反引号 ` 和 CONCAT 函数都是非标准 SQL。

    如果你真的想引用表名,你需要使用双引号。您需要在 Postgresql 中使用名为 || 的文本连接运算符。

    所以你会有类似的东西:

    SELECT "batches".*, (courses.code || '-' || "batches"."name") as course_full_name ...
    

    保持一致 - 如果您在某些地方引用表名,请在任何地方引用它们。

    在您至少基本了解两个数据库的语法之前,这将是缓慢的。不过,由于您使用的是 rails,因此大概没有太多原始 SQL。

    提示:留出几个小时浏览这两个系统的手册。至少你会知道去哪里查看详细信息。

    【讨论】:

    • 感谢您的提示!竖起大拇指
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2013-08-20
    • 2018-10-22
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多