【问题标题】:SqlAlchemy Database IssueSqlAlchemy 数据库问题
【发布时间】:2014-03-11 08:25:00
【问题描述】:

我创建了一个 Bmarks 表,它有两个与同一个表 Url_hash 有关系的外键

class Hashed(Base):
    __tablename__ = "url_hash"

    hash_id = Column(Unicode(22), primary_key=True)
    url = Column(UnicodeText)
    clicks = Column(Integer, default=0)

    def __init__(self, url):
        cleaned_url = str(unidecode(url))
        self.hash_id = unicode(generate_hash(cleaned_url))
        self.url = url

class Bmark(Base):
    __tablename__ = "bmarks"

    bid = Column(Integer, autoincrement=True, primary_key=True)
    hash_id = Column(Unicode(22), ForeignKey('url_hash.hash_id'))
    clean_hash_id = Column(Unicode(22), ForeignKey('url_hash.hash_id'))
    description = Column(UnicodeText())
    extended = Column(UnicodeText())
    stored = Column(DateTime, default=datetime.utcnow)
    updated = Column(DateTime, onupdate=datetime.utcnow)
    clicks = Column(Integer, default=0)
    inserted_by = Column(Unicode(255))
    username = Column(Unicode(255), ForeignKey('users.username'),
                      nullable=False,)
    tag_str = Column(UnicodeText())

    hashed = relation(Hashed,
                      foreign_keys="Bmark.hash_id",
                      backref="bmark",
                      uselist=False
                      )

    clean_hashed = relation(Hashed,
                      foreign_keys="Bmark.clean_hash_id",
                      backref="bmark",
                      uselist=False
                      )

我正在尝试在清理后存储 url,例如删除标题、utm 参数等以用于索引目的 创建数据库时出错

sqlalchemy.exc.ArgumentError: Error creating backref 'bmark' on relationship 'Bmark.clean_hashed': property of that name exists on mapper 'Mapper|Hashed|url_hash'

【问题讨论】:

    标签: python orm sqlalchemy


    【解决方案1】:

    实际上,错误信息非常有用。
    只需将您的 backref="bmark" 之一重命名为 backref="my_clean_bmark" 之类的其他名称。

    【讨论】:

    • 嘿,现在它显示错误“sqlalchemy.exc.OperationalError: (OperationalError) table bmarks has no column named clean_hash_id”任何想法
    • 还有这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 2021-03-27
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2019-10-01
    • 2019-08-26
    • 2020-08-03
    相关资源
    最近更新 更多