实体-(多对一)->相关表-(多对一)-->属性映射不起作用
我在一个相当简单的情况下遇到映射问题。有人可以帮忙吗? 这是 DTO:
class AttributeDTO(object):
id = None
name = None
class RelationDTO(object):
id = None
name = None
attribute = None # one attribute per relation
class EntityDTO(object):
id = None
name = None
relation = None
#it works fine, but then I must get myEnt.relation.attribute,
#when i want to get the attribute from entity
attribute = None # I want it to be placed HERE!
这是映射器:
class TableMapper(object):
def __init__(self, metadata, mapped_type):
self._table = None
self._mapped_type = mapped_type
def get_table(self):
return self._table
def set_table(self, table):
self._table = table
def map_table(self):
mapper(self._mapped_type, self._table)
return self._table
class AttributeTableMapper(TableMapper):
...
class RelationTableMapper(TableMapper):
...
def map_table(self, attribute_table):
r_attribute = relationship(AttributeDTO,
uselist = False, remote_side = [attribute_table.c.id])
mapper(RelationDTO,
self._table,
properties = {'attribute': r_attribute})
return self._table
class EntityTableMapper(TableMapper):
def __init__(self, metadata):
TableMapper.__init__(self, metadata, EntityDTO)
self.set_table(Table('entities', metadata,
Column('id', Integer, ...)))
def map_table(self, relation_table, attribute_table):
r_attribute = relationship(AtributeDTO, uselist = False,
#I TRIED THE FOLLOWING, BUT GOT ERRORS
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
primaryjoin = self._table.c.id_relation == relation.c.id,
secondary = relation_table,
secondaryjoin = relation_table.c.id_attribute == attribute_table.c.id)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mapper(EntityDTO, self._table, properties={'attribute': r_attribute})
return self._table
因此,我映射表:
attribute_t_mapper = AttributeTableMapper(self._metadata)
relation_table = RelationTableMapper(self._metadata).\
map_table(attribute_t_mapper.get_table())
attribute_table = attribute_t_mapper.map_table()
entity_table = EntityTableMapper(self._metadata).\
map_table(relation_table, attribute_table)
并尝试从实体获取我的属性:
entity = session.query(EntityDTO).first() #OK!
a = entity.relation.attribute #OK!
a = entity.attribute #ERROR!
sqlalchemy.orm.exc.UnmappedColumnError: 在映射器 Mapper|EntityDTO|entities 上没有配置列关系.id ...
我做错了什么?
I have mapping problem in a rather simple case. Can anyone help?
Here are the DTOs:
class AttributeDTO(object):
id = None
name = None
class RelationDTO(object):
id = None
name = None
attribute = None # one attribute per relation
class EntityDTO(object):
id = None
name = None
relation = None
#it works fine, but then I must get myEnt.relation.attribute,
#when i want to get the attribute from entity
attribute = None # I want it to be placed HERE!
and here are the mappers:
class TableMapper(object):
def __init__(self, metadata, mapped_type):
self._table = None
self._mapped_type = mapped_type
def get_table(self):
return self._table
def set_table(self, table):
self._table = table
def map_table(self):
mapper(self._mapped_type, self._table)
return self._table
class AttributeTableMapper(TableMapper):
...
class RelationTableMapper(TableMapper):
...
def map_table(self, attribute_table):
r_attribute = relationship(AttributeDTO,
uselist = False, remote_side = [attribute_table.c.id])
mapper(RelationDTO,
self._table,
properties = {'attribute': r_attribute})
return self._table
class EntityTableMapper(TableMapper):
def __init__(self, metadata):
TableMapper.__init__(self, metadata, EntityDTO)
self.set_table(Table('entities', metadata,
Column('id', Integer, ...)))
def map_table(self, relation_table, attribute_table):
r_attribute = relationship(AtributeDTO, uselist = False,
#I TRIED THE FOLLOWING, BUT GOT ERRORS
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
primaryjoin = self._table.c.id_relation == relation.c.id,
secondary = relation_table,
secondaryjoin = relation_table.c.id_attribute == attribute_table.c.id)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mapper(EntityDTO, self._table, properties={'attribute': r_attribute})
return self._table
So, I map the tables:
attribute_t_mapper = AttributeTableMapper(self._metadata)
relation_table = RelationTableMapper(self._metadata).\
map_table(attribute_t_mapper.get_table())
attribute_table = attribute_t_mapper.map_table()
entity_table = EntityTableMapper(self._metadata).\
map_table(relation_table, attribute_table)
and try to get my attribute from the entity:
entity = session.query(EntityDTO).first() #OK!
a = entity.relation.attribute #OK!
a = entity.attribute #ERROR!
sqlalchemy.orm.exc.UnmappedColumnError: No column relation.id is configured on mapper Mapper|EntityDTO|entities...
What am I doind wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论