Correct, I did forget to wrap the array. That works perfect, and the doing
it in the model is exactly what I was looking for after getting this to
work.
Thank you Jeremy!
-Tarik
On Monday, February 9, 2015 at 6:01:33 PM UTC+1, Jeremy Evans wrote:
>
> On Monday, February 9, 2015 at 8:29:21 AM UTC-8, Tarik Ansari wrote:
>>
>> Hello there,
>>
>> I am trying to insert an array of composite type, I have looked at the
>> doc for pg_array and pg_row (and also pg_array_ops and pg_row_ops) and I am
>> uncertain about the process to do it.
>>
>> I have a clients tables with a addresses column which is an array of a
>> composite type address (itself containing an array).
>>
>> First, I load pg_array and pg_row module, define the composite type:
>>
>> DB.extension(:pg_array, :pg_row)
>> DB.register_row_type(:address)
>> clients = DB[:clients]
>>
>> Then, setup my object to insert:
>>
>> address1 = {
>> line1: "34 rue de Paris",
>> line2: null,
>> zip: "75001",
>> coordinates: [48.8567, 2.3508]
>> }
>>
>> client = {
>> name = "Joe",
>> addresses = ? # would be [address1] in a normal ruby object
>> }
>>
>> I have tried these solutions:
>>
>> addresses = Sequel.pg_array(address1, :address)
>>
>> and
>>
>> addresses = Sequel.pg_array(DB.row_type(:address, address1))
>>
>> and
>>
>> addresses = Sequel.pg_array(DB.row_type(:address, address1), :address)
>>
>> But they all result in a malformed queries.
>>
>> Any pointer would be highly appreciated.
>>
>
> There's probably a couple of issues. First, Sequel.pg_array takes an
> array. Second, it looks like address.coordinates is an array of floats, so
> you would need wrap that as well. Here's a self contained example that
> works:
>
> DB.create_table(:address) do
> String :line1
> String :line2
> String :zip
> column :coordinates, 'real[]'
> end
> DB.create_table(:clients) do
> primary_key :id
> String :name
> column :addresses, 'address[]'
> end
>
> DB.extension :pg_array, :pg_row
> Sequel.extension :pg_array_ops
> DB.register_row_type(:address)
>
> DB[:clients].insert(:name=>'Joe',
> :addresses=>Sequel.pg_array([DB.row_type(:address, {:line1=>'34 rue de
> Paris', :line2=>nil, :zip=>'75001', :coordinates=>Sequel.pg_array([48.8567,
> 2.3508])})]))
>
>
> Note that if you use models, the argument wrapping is taken care of for
> you:
>
> class Address < Sequel::Model(:address)
> plugin :pg_row
> end
> class Client < Sequel::Model; end
>
> Client.create(:name=>'Joe', :addresses=>[{:line1=>'34 rue de Paris',
> :line2=>nil, :zip=>'75001', :coordinates=>[48.8567, 2.3508]}])
>
> Thanks,
> Jeremy
>
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.