How to use relationships declared in the model.py ?

12 views
Skip to first unread message

andrew b

unread,
Feb 13, 2013, 3:26:12 PM2/13/13
to turbo...@googlegroups.com
So I see how you can access the columns in one of your model objects. For example ObjectQuery.user_name to get the user_name attribute from that query object. But how do I use the relationships that I setup in my model classes? I made a class definition where I join two tables(and it worked fine, but seems wasteful if there's a better way), because I couldn't figure out how to access the relation from the table below. 

Example code of mine:
class Role(DeclarativeBase):
    __tablename__ = 'roles'

    __table_args__ = {}

    #column definitions
    role_name = Column(u'role_name', VARCHAR(length=45))
    roles_id = Column(u'roles_id', INTEGER(), primary_key=True, nullable=False)

    #relation definitions
    projects = relation('Project', primaryjoin='Role.roles_id==ProjectRole.role_id', secondary=project_roles, secondaryjoin='ProjectRole.project_id==Project.projects_id')

Christoph Zwerschke

unread,
Feb 13, 2013, 3:48:28 PM2/13/13
to turbo...@googlegroups.com
Am 13.02.2013 21:26, schrieb andrew b:
> So I see how you can access the columns in one of your model objects.
> For example ObjectQuery.user_name to get the user_name attribute from
> that query object. But how do I use the relationships that I setup in my
> model classes? I made a class definition where I join two tables(and it
> worked fine, but seems wasteful if there's a better way), because I
> couldn't figure out how to access the relation from the table below.

You should use the ForeignKey construct. Have a look at the SQLAlchemy
docs here, they explain it very well:
http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship

-- Christoph

andrew b

unread,
Feb 14, 2013, 1:50:04 PM2/14/13
to turbo...@googlegroups.com
Hey thanks, I've looked at that before. But everytime I try to use the Object.relationship it gives me a NoneType error says I can't append to it etc. Maybe I'm doing something incorrectly. Here's another example of what I'm trying to do.

CODE IN MY ROOT CONTROLLER: 
    c1 = Component()
    v1 = Version()
    c1.version.append(v1)
   
    print c1.version


CODE IN MY MODEL:
class Component(DeclarativeBase):
    __tablename__ = 'component'

    __table_args__ = {}

    #column definitions
    component_pk = Column(u'component_pk', INTEGER(), primary_key=True, nullable=False)
    version_id = Column(u'version_id', INTEGER(), ForeignKey('version.version_id'), nullable=False)

    #relation definitions
    version = relation('Version', primaryjoin='Component.version_id==Version.version_id', backref="user")
    images = relation('Image', primaryjoin='Component.component_pk==component_has_image.c.component_component_pk', secondary=component_has_image, secondaryjoin='component_has_image.c.image_image_pk==Image.image_pk')
    parts = relation('Part', primaryjoin='Component.component_pk==component_has_part.c.component_component_pk', secondary=component_has_part, secondaryjoin='component_has_part.c.part_part_pk==Part.part_pk')

class Version(DeclarativeBase):
    __tablename__ = 'version'

    __table_args__ = {}

    #column definitions
    object_type = Column(u'object_type', VARCHAR(length=45))
    version_id = Column(u'version_id', INTEGER(), primary_key=True, nullable=False)
    version_name = Column(u'version_name', VARCHAR(length=45))
    #relation definitions
    

Moritz Schlarb

unread,
Feb 14, 2013, 3:20:47 PM2/14/13
to turbo...@googlegroups.com
Your definition implies that there is an one-to-many relationship between Component and Version.
So you can do:
c1.version = v1
Or the other way around:
v1.user.append(c1)
Reply all
Reply to author
Forward
0 new messages