SQLAlchemy JOIN和不明确的列名

时间:2019-01-19 15:54:29

标签: python sql orm sqlalchemy

我有这张桌子

sed -E 's/[0-9]+\".*\x[0-9]+/321\\x38\\x80\\x81\\x8\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8A\\x8B\\x8C\\x8D\\x8E\\x8F\\x90\\x91\\x92\\x93/g'  Input_file
singh is king bla bla 321\x38\x80\x81\x8\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93

当我尝试这样做时:

class Domain(Base):
    __tablename__ = "domains"
    domain_id = Column(Integer(), primary_key=True)
    domain_name = Column(String(), unique=True)


class Subdomain(Base):
    __tablename__ = "subdomains"
    subdomain_id = Column(Integer(), primary_key=True)
    subdomain_name = Column(String(), unique=True)
    domain_id = Column(Integer, ForeignKey('domains.domain_id'))


class Title(Base):
    __tablename__ = "titles"
    title_id = Column(Integer(), primary_key=True)
    domain_id = Column(Integer, ForeignKey('domains.domain_id'))
    subdomain_id = Column(Integer, ForeignKey('subdomains.subdomain_id'))
    title = Column(String())
    status = Column(Integer())
    response_len = Column(Integer())
    created_on = Column(DateTime(), default=datetime.now)
    updated_on = Column(DateTime(), default=datetime.now, onupdate=datetime.now)

我知道了

query = session.query(Domain.domain_name, Subdomain.subdomain_name, Title.title, Title.status, Title.response_len, Title.created_on, Title.updated_on)
query = query.join(Title).join(Subdomain).join(Domain)

最初,我在域中有名称,在子域中有名称,我尝试使用标签-这无济于事。我将其重命名为domain_name和subdomain_name,仍然没有运气。

1 个答案:

答案 0 :(得分:0)

Domain已作为连接FROM domains JOIN ...中最左边的表引入,因为它是您从查询的选择列表中选择的第一个表。显式的非混叠连接是不必要的,并且会导致您的错误。只需忽略它:

query = session.query(Domain.domain_name, Subdomain.subdomain_name, Title.title, Title.status, Title.response_len, Title.created_on, Title.updated_on)
query = query.join(Title).join(Subdomain)