On Wednesday, February 13, 2013 3:48:28 PM UTC-5, Christoph Zwerschke wrote:
>
> 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 
>


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
    

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to