实体-(多对一)->相关表-(多对一)-->属性映射不起作用

发布于 2025-01-01 01:57:08 字数 2740 浏览 0 评论 0原文

我在一个相当简单的情况下遇到映射问题。有人可以帮忙吗? 这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文