Hello sqlalchemy-users,

I  have  some  problems  when  I  try  to use polymorphic objects with
relation one to many (attached file will show problem).

In short words problem is following:
I create a mapper that is inherited from another mapper and I create a
table with "one to many" relation with "base" mapper.

And  when  I  select related objects using relation property I get all
objects  from  "related"  table,  not just those with matching foreign
key. (In case if I expalined poorly, I attached example script to show
this problem).

Am I doing something wrong? Or is that a problem with sqlaclhemy?


-- 
Best regards,
 Vasily                          mailto:[EMAIL PROTECTED]
from sqlalchemy import *
import sys

class SqlStrMixing( object ):
    def __str__( self ):
        s = [ self.__class__.__name__ + ': ' ]
        
        for c in self.c:
            s.append( u'%s=%s ' % ( c.key, getattr(self, c.key) ) )
        result = u''.join(s).encode('ascii')
        return result

db = create_engine('sqlite://', echo=True, echo_uow=False)

people = Table('people', db, 
   Column('person_id', Integer, primary_key=True),
   Column('name', String(50)))
   
engineers = Table('engineers', db,
   Column('person_id', Integer, ForeignKey('people.person_id'), 
primary_key=True),
   Column('status', String(30)),
   Column('engineer_name', String(50)),
   Column('primary_language', String(50)),
  )
   
managers = Table('managers', db, 
   Column('person_id', Integer, ForeignKey('people.person_id'), 
primary_key=True),
   Column('status', String(30)),
   Column('manager_name', String(50))
   )

accounts = Table('accounts', db,
    Column('account_id', Integer, primary_key=True),
    Column('xxperson_id', Integer, ForeignKey('people.person_id')),
    Column('name', String(50)))

people.create()
engineers.create()
managers.create()
accounts.create()

class Person(SqlStrMixing):
    pass

class Engineer(SqlStrMixing):
    pass

class Manager(SqlStrMixing):
    pass

class Account(SqlStrMixing):
    pass

assign_mapper(Account, accounts)

assign_mapper(Person, people,
    properties={
        'accounts' : relation(Account.mapper, private=True) }
    )

assign_mapper(Engineer, engineers, inherits=Person.mapper)
assign_mapper(Manager, managers, inherits=Person.mapper)

john = Engineer(status='Status', engineer_name='John', 
primary_language='Python')
john.accounts.append(Account(name="John's account"))

jack = Engineer(status='Status', engineer_name='Jack', primary_language='Java')
jack.accounts.append(Account(name="Jack's account"))

objectstore.commit()

objectstore.clear()

[john, jack] = Engineer.select()

# Gives incorect results: prints two(!!!) accounts
print john
print 'accounts:'
for acc in john.accounts:
    print acc
    
# Gives incorrect results: prints two(!!!) accounts
print
print jack
print 'accounts:'
for acc in jack.accounts:
    print acc

# Gives correct results: prints one account
print
for acc in Account.select_by(person_id=john.person_id):
    print acc

Reply via email to