6 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: Declaring a relation...
FromSent OnAttachments
Barry Hart24 Oct 2007 13:16 
Michael Bayer24 Oct 2007 13:41 
Barry Hart24 Oct 2007 14:01 
Michael Bayer24 Oct 2007 14:42 
Michael Bayer24 Oct 2007 16:26 
Barry Hart24 Oct 2007 18:58 
Subject:[sqlalchemy] Re: Declaring a relationship twice - could SqlAlchemy auto-detect problems like this?
From:Barry Hart (barr@yahoo.com)
Date:10/24/2007 02:01:57 PM
List:com.googlegroups.sqlalchemy

Here's what I had in mind. This set of mappings compiles without errors in
0.3.11:

from sqlalchemy import * from sqlalchemy.orm import *

engine = create_engine('sqlite://') meta = MetaData(engine)

a = Table('a', meta, Column('id', Integer, primary_key=True)) b = Table('b', meta, Column('id', Integer, primary_key=True), Column('a_id',
Integer, ForeignKey('a.id')))

class A(object):pass class B(object):pass class C(object):pass

mapper(A, a, properties={ 'b':relation(B, backref='a') }) mapper(B, b, properties={ 'a':relation(A, backref='b') })

compile_mappers()

Barry

----- Original Message ---- From: Michael Bayer <mike@zzzcomputing.com> To: sqla@googlegroups.com Sent: Wednesday, October 24, 2007 4:41:46 PM Subject: [sqlalchemy] Re: Declaring a relationship twice - could SqlAlchemy
auto-detect problems like this?

On Oct 24, 2007, at 4:16 PM, Barry Hart wrote:

This subject came up on the TurboGears list and someone suggested I post here.

I noticed a while back that in SqlAlchemy 0.3.x, if you have two mapped classes
A and B, and you define the same relationship (with a backref) on both classes,
you won't get an error message but the two relationships interfere with each
other. For example, you might set the value of the relationship and it won't be
saved to the database. Would it be possible to detect and flag this as an error
at model compilation time?

hey Barry -

that would be a bug, and I cant reproduce it, at least the bug i think youre
describing, in neither 0.3 nor 0.4:

from sqlalchemy import * from sqlalchemy.orm import *

engine = create_engine('sqlite://') meta = MetaData(engine)

a = Table('a', meta, Column('id', Integer, primary_key=True)) b = Table('b', meta, Column('id', Integer, primary_key=True), Column('a_id',
Integer, ForeignKey('a.id'))) c = Table('c', meta, Column('id', Integer, primary_key=True), Column('a_id',
Integer, ForeignKey('a.id')))

class A(object):pass class B(object):pass class C(object):pass

mapper(A, a) mapper(B, b, properties={ 'a':relation(A, backref='thebackref') }) mapper(C, c, properties={ 'a':relation(A, backref='thebackref') })

compile_mappers()

output:

<stack trace> sqlalchemy.exceptions.ArgumentError: Backrefs do not match: backref
'thebackref' expects to connect to <class '__main__.C'>, but found a backref
already connected to <class '__main__.B'>

can you produce a test case ?