xaotuk wrote:
>
> We have tried suggested, but: field 'modified' exists in both parent
> and child tables, when we redefined property 'modified' in mapper with
> something like this:
> mapper(Child, child_table, properties={'modified' =
> child_table.c.modified, ...}), modified field still returned value
> from parent's table.

here is an example illustrating how to move "modified" away as an
attribute name within each mapped class, allowing them to be accessible
separately despite the tables having the same column names.  If you want
to set the parent's "modified" column on the child, use the
"parent_modified" attribute:

from sqlalchemy import *
from sqlalchemy.orm import *

engine = create_engine("sqlite://", echo=True)

m = MetaData()

parent = Table('parent', m,
                Column('id', Integer, primary_key=True),
                Column('type', String),
                Column('modified', String)
)

child = Table('child', m,
                Column('id', Integer, ForeignKey('parent.id'),
primary_key=True),
                Column('modified', String)
)
m.create_all(engine)

class Parent(object):
    def __init__(self, modified):
        self.modified = modified

    def modified(self):
        return self.parent_modified

    def _set_modified(self, m):
        self.parent_modified = m

    modified = property(modified, _set_modified)

class Child(Parent):
    def modified(self):
        return self.child_modified

    def _set_modified(self, m):
        self.child_modified = m

    modified = property(modified, _set_modified)

mapper(Parent, parent, polymorphic_on=parent.c.type,
polymorphic_identity='parent', properties={
    'parent_modified':parent.c.modified
})
mapper(Child, child, inherits=Parent, polymorphic_identity='child',
properties={
    'child_modified':child.c.modified
})


s = sessionmaker(engine)()

p1 = Parent('p1')
c1 = Child('c1')
assert p1.modified == 'p1'
assert c1.modified == 'c1'

s.add_all([p1, c1])
s.commit()
assert p1.modified == 'p1'
assert c1.modified == 'c1'








> We also tried to add property to class like this:
>
> class Parent(object) :
>   table = None
>   ...
>   def _fieldFromQuery(self, field):
>         if not self.table:
>             return None
>         return Session.object_session(self).execute(select([getattr
> (self.table.c, field)]).where(self.table.c.id==self.id)).scalar()
>
>     def _modified(self):
>         return self._fieldFromQuery("modified")
>
>     def _modified_by(self):
>         return self._fieldFromQuery("modified_by")
>
>     modified = property(_modified)
>     modified_by = property(_modified_by)
>
> where self.table is assigned just before mapper is created.
> Mapper itself again redefined attribute 'modified' and 'modified_by'
> so we were back to starting problem.
>
> On Oct 29, 3:56 pm, "Michael Bayer" <[email protected]> wrote:
>> bojanb wrote:
>>
>> > Hi,
>>
>> > Can I have identical column names in both parent and child classes
>> > that are part of a joined-table inheritance? These are simply created,
>> > created_by, modified, modified_by columns that are populated by
>> > defaults defined for them (ie. default, server_default, onupdate).
>>
>> > The values are written to the database correctly, but I have a problem
>> > reading them because parent's column values override child values. So,
>> > if the parent has null values for modified, modified_by and the child
>> > some actual values, ORM returns None for child.modified and
>> > child.modified_by.
>>
>> use the "properties" dictionary to mapper to redefine the names.  or the
>> declarative equivalent.
>>  seehttp://www.sqlalchemy.org/docs/05/mappers.html#customizing-column-pro...
>>
>>
>>
>> > Suggestions?
>
> >
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to