should have this fixed in changeset 1679
its better if you dont use add_property btw, i noticed you were using
a property "Name" which you mean to be "name"....because of
add_property, the People class had both. if you do it this way, it
just has one of them:
mapper(Originals, table_originals, order_by=Originals.order,
properties={
'people': relation(IsAuthor, association=People),
'authors': relation(People,
secondary=table_isauthor, backref='written',
primaryjoin=and_
(table_originals.c.ID==table_isauthor.c.OriginalsID,
table_isauthor.c.Kind=='A')),
'title': table_originals.c.Title,
'date': table_originals.c.Date,
})
mapper(People, table_people, order_by=People.order,
properties= {
'originals': relation(IsAuthor,
association=Originals),
'name': table_people.c.Name,
'country': table_people.c.Country,
})
mapper(IsAuthor, table_isauthor,
primary_key=[table_isauthor.c.OriginalsID,
table_isauthor.c.PeopleID,
table_isauthor.c.Kind],
properties={
'original': relation(Originals, lazy=False),
'person': relation(People, lazy=False),
'kind': table_isauthor.c.Kind,
})
then People is constructed as:
p = People(name='name', country='es')
On Jun 29, 2006, at 6:45 PM, Raul Garcia wrote:
> Michael Bayer posted this on gmane.comp.python.sqlalchemy.user:
>
>> heres how im running it:
>>
>> z-eeks-Computer:~/dev/sqlalchemy classic$ export PYTHONPATH=./lib/:./
>> test/
>> z-eeks-Computer:~/dev/sqlalchemy classic$ python threadlocal-
>> association.py
>> testdelete (__main__.AssociationTest) ... ok
>> testinsert (__main__.AssociationTest) ... ok
>> testmodify (__main__.AssociationTest) ... ok
>> testreplace (__main__.AssociationTest) ... ok
>
> The test doesn't pass on my Windows setup, neither with sqlite nor
> with
> PostgreSQL. But, oddly enough, I've tested on a linux machine and
> it works
> like a charm. But my model code still doesn't work in any of the
> setups.
>
> So I've made a very little test with a portion of my model code.
> The test
> doesn't work, and I've tried with threadlocal, without it, usind
> Bound and
> DynamicMetadata, and usinf sqlite and PostgreSQL engines, to no avail.
>
> The culprits are the relationships which use an association object;
> if I
> delete them, all the model works, but defining them makes the code
> to fail.
>
> Here is the test code:
> ----------------------------------------------------------------------
> ---
> import testbase
>
> from sqlalchemy import *
>
> class AssociationTest(testbase.PersistTest):
> def setUpAll(self):
> self.install_threadlocal()
> global table_originals, table_people, table_isauthor,
> metadata,
> Originals, People, IsAuthor
> metadata = BoundMetaData(testbase.db)
> table_originals = Table('Originals', metadata,
> Column('ID', Integer, primary_key=True),
> Column('Title', String(200), nullable=False),
> Column('Date', Date ),
> )
> table_people = Table('People', metadata,
> Column('ID', Integer, primary_key=True),
> Column('Name', String(140), nullable=False),
> Column('Country', CHAR(2), default='es'),
> )
> table_isauthor = Table('IsAuthor', metadata,
> Column('OriginalsID', Integer, ForeignKey('Originals.ID'),
> default=None),
> Column('PeopleID', Integer, ForeignKey('People.ID'),
> default=None),
> Column('Kind', CHAR(1), default='A'),
> )
> metadata.create_all()
>
> class Base(object):
> def __init__(self, **kw):
> for k,v in kw.iteritems():
> setattr(self, k, v)
> def display(self):
> c = [ "%s=%s" % (col.key, repr(getattr(self, col.key))) for col
> in self.c ]
> return "%s(%s)" % (self.__class__.__name__, ', '.join(c))
> def __repr__(self):
> return self.display()
> def __str__(self):
> return self.display()
> class Originals(Base):
> order = [table_originals.c.Title, table_originals.c.Date]
> class People(Base):
> order = [table_people.c.Name]
> class IsAuthor(Base):
> pass
>
> assign_mapper(Originals, table_originals, order_by=Originals.order)
> assign_mapper(People, table_people, order_by=People.order)
> assign_mapper(IsAuthor, table_isauthor, primary_key=
> [table_isauthor.c.OriginalsID, table_isauthor.c.PeopleID,
> table_isauthor.c.Kind])
>
> Originals.mapper.add_properties({
> 'people': relation(IsAuthor, association=People),
> 'authors': relation(People, secondary=table_isauthor,
> backref='written',
> primaryjoin=and_(Originals.c.ID==table_isauthor.c.OriginalsID,
> table_isauthor.c.Kind=='A')),
> 'title': table_originals.c.Title,
> 'date': table_originals.c.Date,
> })
> People.mapper.add_properties({
> 'originals': relation(IsAuthor, association=Originals),
> 'name': table_people.c.Name,
> 'country': table_people.c.Country,
> })
> IsAuthor.mapper.add_properties({
> 'original': relation(Originals, lazy=False),
> 'person': relation(People, lazy=False),
> 'kind': table_isauthor.c.Kind,
> })
>
> def tearDown(self):
> for t in metadata.table_iterator(reverse=True):
> t.delete().execute()
> def tearDownAll(self):
> clear_mappers()
> metadata.drop_all()
>
> def testinsert(self):
> p = People(Name='name', Country='es')
> objectstore.flush()
>
>
> if __name__ == "__main__":
> testbase.main()
> ----------------------------------------------------------------------
> ---
>
> Regards,
>
>
> Raul Garcia.
>
>
> Using Tomcat but need to do more? Need to support web services,
> security?
> Get stuff done quickly with pre-integrated technology to make your
> job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache
> Geronimo
> http://sel.as-us.falkag.net/sel?
> cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Sqlalchemy-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users