[sqlalchemy] No attribute of child relation contained in parent object

2011-01-26 Thread cornelinux
Hi,

I am just starting to use the relation between two tables.
The problem started with the child data not being written to the child
tables.
I played around, and now I am totally confused.
Maybe someone can shed some light on this.

I got these tables:

{{{
user_table = sa.Table('User', meta.metadata,
sa.Column('UserId',  sa.types.Integer(),
primary_key=True),
sa.Column('UserDesc',sa.types.UnicodeText(),
default=u''),
)

userrealm_table = sa.Table('UserRealm', meta.metadata,
sa.Column('id', sa.types.Integer(), primary_key=True),
sa.Column('user_id',
sa.types.Integer(),ForeignKey('User.UserId')),
sa.Column('realm_id', sa.types.Integer(),
nullable=False )
)
}}}

I got the following classes:

{{{
class User(object):
def __init__(self,  desc):
log.debug(' __init__(%s)' % desc)
self.UserDesc = serial

classe UserRealm(object):
def __init__(self, realm):
log.debug(setting realm_id to %i % realm)
self.realm_id = realm
}}}

A user may belong to several realms. All the relation stuff should be
done in the mappers:

{{{
orm.mapper(UserRealm, userrealm_table)
orm.mapper(User, user_table, properties={
'children':relationship(UserRealm,backref='user', cascade=save-
update)
})
}}}

Now I am at the point, that the User object contains no attribute
identifying the realm...
I thought this attribute should be generated by the relation
definition?

Any ideas on this?

Kind regards
Cornelius

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] No attribute of child relation contained in parent object

2011-01-26 Thread Cornelius Kölbel
Hi List,

ok, i figured it out. My understanding of the documentation was a bit
different.
I really worked out this way. But

Thekey in the properties is the attribute name, that will be added to
the parent object.
So doing this

orm.mapper(User, user_table, properties={
'realms':relation(TokenRealm, backref=backref('user')
)

...will add an attribute User.realms.

Kind regards
Cornelius

Am 26.01.2011 11:45, schrieb cornelinux:
 Hi,

 I am just starting to use the relation between two tables.
 The problem started with the child data not being written to the child
 tables.
 I played around, and now I am totally confused.
 Maybe someone can shed some light on this.

 I got these tables:

 {{{
 user_table = sa.Table('User', meta.metadata,
 sa.Column('UserId',  sa.types.Integer(),
 primary_key=True),
 sa.Column('UserDesc',sa.types.UnicodeText(),
 default=u''),
 )

 userrealm_table = sa.Table('UserRealm', meta.metadata,
 sa.Column('id', sa.types.Integer(), primary_key=True),
 sa.Column('user_id',
 sa.types.Integer(),ForeignKey('User.UserId')),
 sa.Column('realm_id', sa.types.Integer(),
 nullable=False )
 )
 }}}

 I got the following classes:

 {{{
 class User(object):
 def __init__(self,  desc):
 log.debug(' __init__(%s)' % desc)
 self.UserDesc = serial

 classe UserRealm(object):
 def __init__(self, realm):
 log.debug(setting realm_id to %i % realm)
 self.realm_id = realm
 }}}

 A user may belong to several realms. All the relation stuff should be
 done in the mappers:

 {{{
 orm.mapper(UserRealm, userrealm_table)
 orm.mapper(User, user_table, properties={
 'children':relationship(UserRealm,backref='user', cascade=save-
 update)
 })
 }}}

 Now I am at the point, that the User object contains no attribute
 identifying the realm...
 I thought this attribute should be generated by the relation
 definition?

 Any ideas on this?

 Kind regards
 Cornelius





signature.asc
Description: OpenPGP digital signature