SqlAlchemy: is it possible to use a hybrid expression in a relationship?
I'm trying to build a relationship in a model where the primaryjoin will use the @.expression (hybrid expression) attribute of the remote class. The code looks like this:
class LCProduct(db.Model):
__tablename__ = 'lcproduct'
product_id = Column(Integer,
Sequence('lcproductcategory_seq'),
primary_key=True)
label = Column(String)
category = relationship('LCCategory', primaryjoin='LCProduct.label == LCCategory.label')
class LCCategory(db.Model):
__tablename__ = 'lccategory'
category_id = Column(Integer, primary_key=True)
category_type = Column(String)
display_name = Column(String)
@hybrid_property
def label(self):
retval = '{category_type}: {display_name}'
.format(category_type=self.category_type, display_name=self.display_name)
return retval
@label.expression
def label(cls):
return func.concat(cls.category_type, func.concat(': ', cls.display_name))
In the above code the LCProduct class has a column 'label' that's just a string. This string is equal to the two columns in LCCategory in this fashion:
{category_name}: {display_name}
the two columns joined with a ': ' between them. This is what the @label.expression does. However, when I run the program that contains this code (the relationship in LCProduct), I get a long error message like this:
InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|LCProductCategory|lcproductcategory'. Original exception was: Could not locate any relevant foreign key columns for primary join condition 'lcproductcategory.label = concat(lccategory.category_type, concat(:concat_1, lccategory.display_name))' on relationship LCProductCategory.category. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation.
Is what I'm trying to do even possible?
Thanks in advance for your help!
Doug
链接地址: http://www.djcxy.com/p/63440.html上一篇: iOS WiFi网络切换