生产环境出现PG::DatatypeMismatch错误,本地SQL环境正常求指导
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:
If you want all Program records (the most likely case for a dropdown):
ReplaceProgram.where('id')withProgram.all—this is explicit and clean:<%= f.collection_select :program_id, Program.all, :id, :name, {}, {class: "input-md form-control mb-20" } %>If you specifically need programs with non-null ids (though primary keys like
idare 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

