0

我有一個遷移創建一個表(只是爲了簡化問題),看起來像這樣:JRuby中使用JDBC的Postgres的Rails(8.4)忽略默認值

create_table :test_defaults, :force => true do |table| 
    table.integer :failure,  :default => -1, :null => false 
end 

所以,一列名爲失敗。當我執行這個命令導軌:

td = TestDefault.create! 

JRuby的失敗:

INSERT INTO "test_defaults" ("failure") VALUES(-1) RETURNING "id" 

我運行:

ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: ERROR: null value in column 
"failure" violates not-null constraint: INSERT INTO "test_defaults" ("failure") 
VALUES(NULL) RETURNING "id" 

而紅寶石版本,像這樣的查詢成功

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) 

建議a重新讚賞。無法找到任何有用的谷歌,甚至不知道誰報告的錯誤。

乾杯。

編輯:哈。似乎它只發生在負面默認情況下。零和更大是好的。

EDIT2:從我下面的回答可以看出,在postgres 8.4中,這是一個圍繞負數默認值括號的代碼問題。如果有人可以在不等待寶石升級的情況下提出解決此問題的方法(例如告訴postgres刪除這些括號),那麼將非常感激。

回答

0

好的。發現這個問題的答案之一:

activerecord-jdbc-adapter-1.1.0/lib/arjdbc/postgresql/adapter.rb 

我們有行(@ 59):

def default_value(value) 
    # Boolean types 
    return "t" if value =~ /true/i 
    return "f" if value =~ /false/i 

    # Char/String/Bytea type values 
    return $1 if value =~ /^'(.*)'::(bpchar|text|character varying|bytea)$/ 

    # Numeric values 
    return value if value =~ /^-?[0-9]+(\.[0-9]*)?/ 

    # Fixed dates/timestamp 
    return $1 if value =~ /^'(.+)'::(date|timestamp)/ 

    # Anything else is blank, some user type, or some function 
    # and we can't know the value of that, so return nil. 
    return nil 
    end 
end 

當從返回的Postgres負默認值,則返回,說,"(-1)"括號內。但是,上面的數字正則表達式在括號中不匹配。

試圖註冊以記錄一個錯誤(在http://kenai.com/jira/browse/ACTIVERECORD_JDBC上),但服務器在註冊過程中失敗。

編輯:創建錯誤:http://kenai.com/jira/browse/ACTIVERECORD_JDBC-151

編輯:也結束了這需要在同一個地方的前面的代碼。 "(-1)".to_i返回0,通常不好。

def type_cast(value) 
     case type 
      when :integer then value = remove_brackets_from_negative(value) 
     end 
     super 
    end 
    def remove_brackets_from_negative(value) 
     value.gsub!(/[\)\(]/,"") if value.is_a?(String) 
     value 
    end 
+0

將此標記爲「未回答」,因爲我對目前的解決方案並不滿意。 – Glenn 2011-03-14 03:49:02