[Dbix-class] Difference between has_many and might_have

Jess Robinson castaway at desert-island.me.uk
Sat Apr 21 12:37:25 GMT 2007




On Mon, 16 Apr 2007, Priya Sivaraman wrote:

>
> I have defined a list of tables that has some relation to each other. At
> the beginning I was using has_many to define some relation between
> table1 and table2. The relation between table1 and table2 might be for
> each record in table1 there can be 0 or more records in table2. For that
> I use to have the has_many relationship.
>
>
>
> Now when I am trying to use the copy command to make a copy of record in
> table1 I am having problem. It throws error telling "Cannot insert the
> record in table2 due to violation of UNIQUE constraint";
>

The copy command will copy related objects from has_many as well, unless 
you set cascade_copy => 1 on the relationship.

The copy should have given your new object a new ID though, so how did you 
get unique constraint problems?

>
> Now I tried using might_have it is not throwing error.

Yup, that one doesn't cascade_copy..

> I would like to know the impact of changing this has_many to might_have.
> Because I used dev server and did the testing. Now when I will move the
> code to live I would like to know whether this code will have any
> impact.

I don't think so.. probably better to use has_many and turn off the copy 
though, or fix the unique problem.

Jess




More information about the Dbix-class mailing list