JRuby on Rails using jdbc postgres (8.4) ignores Default values
-
30-10-2019 - |
Question
I have a table created in a migration (only to simplify the problem) that looks like this:
create_table :test_defaults, :force => true do |table|
table.integer :failure, :default => -1, :null => false
end
So, one column named failure. When I execute this rails command:
td = TestDefault.create!
JRuby fails with:
ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: ERROR: null value in column
"failure" violates not-null constraint: INSERT INTO "test_defaults" ("failure")
VALUES(NULL) RETURNING "id"
whereas the ruby version succeeds with a query like this:
INSERT INTO "test_defaults" ("failure") VALUES(-1) RETURNING "id"
I'm running:
ruby 1.8.7,
rails 2.2.2,
jruby 1.5.6,
activerecord-jdbc-adapter (1.1.0)
activerecord-jdbcpostgresql-adapter (1.1.0)
jdbc-postgres (8.4.702)
Suggestions are appreciated. Haven't been able to find anything helpful on google, and don't even know who to report the bug to.
Cheers.
EDIT: Ha. Seems it only happens with negative defaults. Zero and greater is fine.
EDIT2: From my answer below, it's a code problem around brackets on negative default values in postgres 8.4. If someone can suggest a way around this issue without waiting for a gem upgrade (like telling postgres to remove those brackets), it would be much appreciated.
No correct solution