postgresql - Rails 3 ActiveRecord save() Method and Autoincrement Primary Key Fields -
i migrated rails site running on 2.2.2 rails 3.1.
i noticed now, on rails 3 save() calls (inserts) used work in 2.2.2 don't in 3.1
the id field in database primary key has following properties:
not null
auto-increment
now, when save() method runs on these tables, get:
activerecord::statementinvalid (pg::error: error: null value in column "id" violates not-null constraint
hmm looked @ generated sql save() creates , indeed it's including id field in column list , assigning nil:
pg::error: error: null value in column "id" violates not-null constraint : insert "server_updates" ("action", "created_at", "field_number", "id", "status", "table_number", "value") values ($1, $2, $3, $4, $5, $6, $7) returning "id"
so, question how activerecord not include id column when generates sql save() call?
i don't want remove not null rule column nor want rewrite of these save() calls raw sql queries.
adding set_primary_key "id"
models fixes issue seems messy , un-rails/ruby me
what work around this?
thanks helping
here 1 of models getting error:
class serverupdate < activerecord::base def serverupdate.run_code code su = serverupdate.new su.action = 3 # run code su.value = code su.status = 1 su.save end def serverupdate.new_insert_for_table table su = serverupdate.new su.action = 1 # create new record su.table_number = table.to_i su.save return su end def serverupdate.new_update_for_table_where_field_equals_value table, field, value su = serverupdate.new su.action = 2 # update record su.table_number = table.to_i su.field_number = field.to_i su.value = value.to_s su.status = 1 ## used in process() method below su.save return su end def serverupdate.new_delete_for_table_where_field_equals_value table, field, value su = serverupdate.new su.action = 999 # delete record su.table_number = table.to_i su.field_number = field.to_i su.value = value.to_s su.save return su end def set_value_for_field value, field, behavior=0 sui = serverupdateitem.new sui.server_update_id = self.id sui.field_number = field.to_i sui.value = value.to_s b = 0 b = 1 if behavior == "prepend" or behavior == 1 b = 2 if behavior == "append" or behavior == 2 sui.behavior = b sui.save end def process setting status=1 in method: serverupdate.new_update_for_table_where_field_equals_value table, field, value self.status = 1 self.save end end
adding set_primary_key "id"
models fixes issue seems messy , unnecessary me