jackc / surus

PostgreSQL extensions for ActiveRecord
MIT License
395 stars 35 forks source link

Integer Array serialization doesn't seem to work - incorrect SQL generated #3

Closed gokulj closed 11 years ago

gokulj commented 11 years ago

I added an integer[] column called 'deleted_by' to a model and used

serialize :deleted_by, Surus::Array::IntegerSerializer.new

but, when i tried assigning an array to it, the generated SQL is not correct (see below).

1.9.3p194 :002 > m.deleted_by = [1,2] => [1, 2] 1.9.3p194 :003 > m.save (0.3ms) BEGIN (0.8ms) UPDATE "messages" SET "deleted_by" = 0, "updated_at" = '2012-09-15 06:15:33.998480' WHERE "messages"."id" = 1136 (0.3ms) ROLLBACK ActiveRecord::StatementInvalid: PG::Error: ERROR: column "deleted_by" is of type integer[] but expression is of type integer LINE 1: UPDATE "messages" SET "deleted_by" = 0, "updated_at" = '2012... ^ HINT: You will need to rewrite or cast the expression. : UPDATE "messages" SET "deleted_by" = 0, "updated_at" = '2012-09-15 06:15:33.998480' WHERE "messages"."id" = 1136

jackc commented 11 years ago

Can you post a more complete test case demonstrating the problem? I am unable to reproduce the problem with the simple test case below.

Here is the database table I used:

CREATE TABLE messages(
  id serial PRIMARY KEY,
  text varchar not null,
  deleted_by integer[]
);

Here is the test code:

require 'active_record'
require 'surus'

ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'surus_integer_array_test'

class Message < ActiveRecord::Base
  serialize :deleted_by, Surus::Array::IntegerSerializer.new
end

m = Message.create! text: 'hello'
m.deleted_by = [1,2]
m.save!

p m

And the output:

#<Message id: 2, text: "hello", deleted_by: [1, 2]>