On Wed, Apr 11, 2018 at 7:40 AM, Tolstov Sergey <whistler...@gmail.com> wrote:
> I use dynamic constructor for class.
> It works fine for instance who have not foreign keys. But when FK on another
> instance it cannot load them
>
> Example:
> class left (Base):
>   id = sqlalchemy.Column(UUID, primary_key = True)
>   def __getattr__(self, attribute):
>     if attribute == 'rights':
>       right().left
>       return getattr(self, attribute)
>
> class right(Base):
>   id = sqlalchemy.Column(UUID, primary_key = True)
>   def __getattr__(self, attribute):
>     if attribute == 'left':
>       rel_key = sqlalchemy.Column(UUID, sqlalchemy.ForeignKey(left.mRID),
> nullable = True)
>       setattr(self.__class__, 'left_mRID', rel_key)
>       setattr(self.__class__, 'left', sqlalchemy.orm.relationship(left,\
>                foreign_keys = rel_key, uselist = False,\
>                backref = sqlalchemy.orm.backref('rights', uselist = True)))
>       return getattr(self,attribute)
>
> Works:
> left_1 = session.query(left).first()
> print (str(left_1.rights))
>
> Not works:
> right_1 = session.query(right).first()
> print (str(right_1.left))
> I think that it may be fix them, but i need universal for all
> relationshipproperties
> class right(Base):
>   ...
>   def __getattr__(self, attribute):
> ...
>     session.refresh(self, attribute_names = ['left_mRID'])
>

I don't know what's going on here, but this isn't really the way
SQLAlchemy is expected to work. Columns and relationships are normally
set up statically, and SQLAlchemy has a "compile mappers" phase which
figures out the connections between all the classes, which you might
be bypassing by defining them dynamically.

Can you explain why you are trying to create columns and relationships
dynamically? There might be a different way to approach the problem.

Simon

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to