[sqlalchemy] Re: Duplication of rows in many-to-many relationship
By chance, in your mappers, are you declaring two relationships instead of one relation with a backref? As a side note, once you straighten this out, you may want to declare the composite (a_id, b_id) as a unique key on the relation table. Barry - Original Message From: Karlo Lozovina [EMAIL PROTECTED] To: sqlalchemy sqlalchemy@googlegroups.com Sent: Sunday, May 4, 2008 4:31:55 PM Subject: [sqlalchemy] Duplication of rows in many-to-many relationship Let's say I have two classes A and B, and I want instances of both classes, to have a list of each other, that is, many-to-many relationship. For a shorthand, a means instance of A, and b is an instance of B. For example: a.bs is a list, full of instances of class B. Similarly, b.as is a list, full of instances of class A. In modelling that relationship I use three tables, one for As, one for Bs, and one for their relationship. If I only append instances of B to some a.bs, then save all those objects, everything works fine. But if I append instances of A and B, both to a.bs and b.as, then save, I get double rows in the third table. Is there a way around that? P.S. In a very likely case I haven't been completely understood, I'll attach some code to demonstrate my point ;). Thanks all. Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Duplication of rows in many-to-many relationship
On May 4, 10:40 pm, Barry Hart [EMAIL PROTECTED] wrote: By chance, in your mappers, are you declaring two relationships instead of one relation with a backref? Yep, that was it, thanks! Should have read the docs more carefully ;). As a side note, once you straighten this out, you may want to declare the composite (a_id, b_id) as a unique key on the relation table. I'll look into it... Thanks for a speedy reply ;). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Duplication of rows in many-to-many relationship
I thought that defining relation with a backref was just a convenient shorthand for defining two relations. This makes it sound like are practical differences between the two techniques. Is this true? What are the differences? Also, does having the unique key that you recommend stop SA from trying to add the duplicate? Or will it try anyway and then get a SQL exception due to the violated constraint? I am often doing if a not in b.as: b.as.append(a) and I have been wondering if there is a way to just do: b.as.append(a) and have SA automatically check if it was already in collection and shouldn't be added again. On Sun, May 4, 2008 at 4:40 PM, Barry Hart [EMAIL PROTECTED] wrote: By chance, in your mappers, are you declaring two relationships instead of one relation with a backref? As a side note, once you straighten this out, you may want to declare the composite (a_id, b_id) as a unique key on the relation table. Barry - Original Message From: Karlo Lozovina [EMAIL PROTECTED] To: sqlalchemy sqlalchemy@googlegroups.com Sent: Sunday, May 4, 2008 4:31:55 PM Subject: [sqlalchemy] Duplication of rows in many-to-many relationship Let's say I have two classes A and B, and I want instances of both classes, to have a list of each other, that is, many-to-many relationship. For a shorthand, a means instance of A, and b is an instance of B. For example: a.bs is a list, full of instances of class B. Similarly, b.as is a list, full of instances of class A. In modelling that relationship I use three tables, one for As, one for Bs, and one for their relationship. If I only append instances of B to some a.bs, then save all those objects, everything works fine. But if I append instances of A and B, both to a.bs and b.as, then save, I get double rows in the third table. Is there a way around that? P.S. In a very likely case I haven't been completely understood, I'll attach some code to demonstrate my point ;). Thanks all. Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Duplication of rows in many-to-many relationship
With a backref, both ends of the relationship are aware of each other -- if you append object b1 to the collection a.b, then a backref b1.a will be updated immediately. If you had two different relationships, you wouldn't see an update to b1.a until you reload the object. It seems like it'd be nice if they worked the same -- perhaps there's a good reason it doesn't. I think 0.4 has an error check for some cases of mutual relationships (A-B, B-A). Maybe the check is not in place for M:N relationships? Barry - Original Message From: Bobby Impollonia [EMAIL PROTECTED] To: sqlalchemy@googlegroups.com Sent: Sunday, May 4, 2008 5:28:56 PM Subject: [sqlalchemy] Re: Duplication of rows in many-to-many relationship I thought that defining relation with a backref was just a convenient shorthand for defining two relations. This makes it sound like are practical differences between the two techniques. Is this true? What are the differences? Also, does having the unique key that you recommend stop SA from trying to add the duplicate? Or will it try anyway and then get a SQL exception due to the violated constraint? I am often doing if a not in b.as: b.as.append(a) and I have been wondering if there is a way to just do: b.as.append(a) and have SA automatically check if it was already in collection and shouldn't be added again. On Sun, May 4, 2008 at 4:40 PM, Barry Hart [EMAIL PROTECTED] wrote: By chance, in your mappers, are you declaring two relationships instead of one relation with a backref? As a side note, once you straighten this out, you may want to declare the composite (a_id, b_id) as a unique key on the relation table. Barry - Original Message From: Karlo Lozovina [EMAIL PROTECTED] To: sqlalchemy sqlalchemy@googlegroups.com Sent: Sunday, May 4, 2008 4:31:55 PM Subject: [sqlalchemy] Duplication of rows in many-to-many relationship Let's say I have two classes A and B, and I want instances of both classes, to have a list of each other, that is, many-to-many relationship. For a shorthand, a means instance of A, and b is an instance of B. For example: a.bs is a list, full of instances of class B. Similarly, b.as is a list, full of instances of class A. In modelling that relationship I use three tables, one for As, one for Bs, and one for their relationship. If I only append instances of B to some a.bs, then save all those objects, everything works fine. But if I append instances of A and B, both to a.bs and b.as, then save, I get double rows in the third table. Is there a way around that? P.S. In a very likely case I haven't been completely understood, I'll attach some code to demonstrate my point ;). Thanks all. Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Duplication of rows in many-to-many relationship
On May 4, 2008, at 8:59 PM, Barry Hart wrote: With a backref, both ends of the relationship are aware of each other -- if you append object b1 to the collection a.b, then a backref b1.a will be updated immediately. If you had two different relationships, you wouldn't see an update to b1.a until you reload the object. It seems like it'd be nice if they worked the same -- perhaps there's a good reason it doesn't. I think 0.4 has an error check for some cases of mutual relationships (A-B, B-A). Maybe the check is not in place for M:N relationships? we dont really have any check of A-B / B-A being present without a backref. the backref is technically not needed for a bidirectional o2m/m2o relationship, unless the post_update option is being used. for the m2m, its explicitly needed since only one side needs to handle the association table, and the other side needs to be made aware of that. we've yet to try working up some alarms for this sort of thingits probably not that hard to do so (for every secondary table, look it up in a registry, see if its mapped, etc). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Duplication of rows in many-to-many relationship
My fault - when I said there was an error check, it was for the case where you declare relationships A-B and B-A both with backrefs. Here's the thread from about six months ago: http://groups.google.com/group/sqlalchemy/browse_thread/thread/420b7de79119ad4d/8e8311bfd18d05e2?lnk=gstq=barry+hart#8e8311bfd18d05e2). Barry - Original Message From: Michael Bayer [EMAIL PROTECTED] To: sqlalchemy@googlegroups.com Sent: Sunday, May 4, 2008 10:31:10 PM Subject: [sqlalchemy] Re: Duplication of rows in many-to-many relationship On May 4, 2008, at 8:59 PM, Barry Hart wrote: With a backref, both ends of the relationship are aware of each other -- if you append object b1 to the collection a.b, then a backref b1.a will be updated immediately. If you had two different relationships, you wouldn't see an update to b1.a until you reload the object. It seems like it'd be nice if they worked the same -- perhaps there's a good reason it doesn't. I think 0.4 has an error check for some cases of mutual relationships (A-B, B-A). Maybe the check is not in place for M:N relationships? we dont really have any check of A-B / B-A being present without a backref. the backref is technically not needed for a bidirectional o2m/m2o relationship, unless the post_update option is being used. for the m2m, its explicitly needed since only one side needs to handle the association table, and the other side needs to be made aware of that. we've yet to try working up some alarms for this sort of thingits probably not that hard to do so (for every secondary table, look it up in a registry, see if its mapped, etc). Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---