You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生产环境出现PG::DatatypeMismatch错误,本地SQL环境正常求指导

Fixing PG::DatatypeMismatch Error in Your Rails Form

Hey there, let's break down why you're hitting that PG::DatatypeMismatch error in production, and how to fix it right away.

The Root Cause

PostgreSQL is strict about data type consistency, unlike MySQL which allows loose implicit conversions. The problematic line in your form is this:

<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>

The Program.where('id') part is tripping up PostgreSQL. In MySQL, writing WHERE id treats any non-zero integer id as a boolean true, so it returns all programs with a valid id. But PostgreSQL expects a proper boolean expression in the WHERE clause—passing an integer directly throws the "argument must be type boolean" error you're seeing.

The Fixes

Depending on your actual intent, here are the correct ways to rewrite this:

  1. If you want all Program records (the most likely case for a dropdown):
    Replace Program.where('id') with Program.all—this is explicit and clean:

    <%= f.collection_select :program_id, Program.all, :id, :name, {}, {class: "input-md form-control mb-20" } %>
    
  2. If you specifically need programs with non-null ids (though primary keys like id are rarely null):
    Use explicit boolean logic that PostgreSQL understands:

    <%= f.collection_select :program_id, Program.where.not(id: nil), :id, :name, {}, {class: "input-md form-control mb-20" } %>
    

Why This Worked Locally

MySQL's lenient type conversion lets WHERE id slide—it automatically converts non-zero integers to boolean true. PostgreSQL rejects this because it enforces strict type safety, which is actually a win for production data integrity!

Your Program has_many :courses association won't be affected by this fix at all—it just ensures the dropdown loads the correct program list without triggering a type mismatch error.

内容的提问来源于stack exchange,提问作者Penny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:12:06