Apparently, the reason why updates won't save is a sequence issue as
found here: 
http://railspikes.com/2009/3/6/duplicate-key-violates-unique-constraint

So I suppose I need to do something like this: select
setval('children_id_seq', (select max(id) + 1 from children));

I have two questions as I am not a DBA:

1) When exactly do I do this? Every time I want to insert a child
record?
2) Why doesn't DataMapper handle this? Maybe I am spoiled by using
Hibernate in the Java world, but they handle sequences fairly easily
by using what they call dialects. I assumed the database-specific
DataMapper gems would do something similar. Or maybe they do and I am
just missing it?

Thanks.


On Jan 13, 1:14 pm, Neil Chaudhuri <[email protected]> wrote:
> I have been able to create a 1:M relationship using DataMapper and
> update the child items as well after creation. However, I have been
> unable to add to that collection.
>
> Let's say I have
>
> class Parent
>   include DataMapper::Resource
>
>   property :id, Serial
>   ...
>
>   has n, :children
> end
>
> class Child
>   include DataMapper::Resource
>
>   property :id, Serial
>   ...
>   belongs_to :parent
> end
>
> Whenever I try to add to the child collection belonging to a certain
> parent instance, I get either DataMapper::UpdateConflictError or
> "DataObjects::IntegrityError: ERROR:  duplicate key value violates
> unique constraint "children_pkey" DETAIL:  Key (id)=(1) already
> exists.
>
> I have tried a whole host of approaches. The most promising seemed
> parent.children.create(attributes), but that led to the
> IntegrityError. What is the preferred approach for adding a new item
> to the collection?
>
> Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to