James
James

Reputation: 6499

Postgres insert error in ActiveRecord

For some reason, I cannot get ActiveRecord to correctly format the insert statement when using an array type column. It seems to want to escape the Postgres notation:

{"val1", "val2", "val3"} into \{\"val1\", \"val2\", \"val3\"\}

Resulting in an error:

PG::Error: ERROR: array value must start with "{" or dimension information

Am I running my db commands wrong? rake db:seed and bundle exec rake db:seed cause this error as well as running migrations.

I'm running Rails 3.2.13 and Postgres 9.3.1

Upvotes: 2

Views: 1068

Answers (1)

mu is too short
mu is too short

Reputation: 434585

The Rails3 version of ActiveRecord doesn't understand PostgreSQL arrays natively so it is falling back to "I don't know what it is so I'll pretend it is a string" mode. If you install postgres_ext then you'll be able to use arrays properly:

Model.where(:some_array_column => [2, 3, 5, 6, 11]).to_sql
# SELECT "models".* FROM "models" WHERE "models"."some_array_column" = '{2,3,5,6,11}'

and inserting a %w[val1 val2 val3] array should work similarly.

Upvotes: 2

Related Questions