python - SQLAlchemy - 某些表类的 ModuleMarker 错误

标签 python postgresql sqlalchemy

我有一个简单的类,它访问一个用于存储运行时间的表。 但是,当我尝试导入该类时,出现以下错误。

AttributeError: '_ModuleMarker' object has no attribute 'add_item'

我已经能够确定它与设置表名和列有关,因为我有 2 个其他类访问两个不同的表并且在那里工作正常。 我基本上是从 2 个原始类中复制粘贴代码,所以我不确定为什么它不起作用。

有没有办法生成更有用的错误代码?

运行时类

from sqlalchemy import Column
from sqlalchemy import String
from sqlalchemy import TIMESTAMP
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('postgresql+psycopg2://postgres@localhost/reseller', encoding='utf-8', pool_size=50, max_overflow=200)
session = sessionmaker(bind=engine)()

Base = declarative_base()

class RunTimes(Base):
    __tablename__ = 'run_times'
    store = Column(String, primary_key=True, index=True, unique=True)
    start_time = Column(TIMESTAMP)
    end_time = Column(TIMESTAMP)

    def __init__(self, store):
        self.store = store
        results = session.query(RunTimes).filter(RunTimes.store.like(store)).first()
        self.start_time = results.start_time
        self.end_time = results.end_time

    def Update(self):
        session.merge(self)
        session.commit()

测试.py

from RunTimes.RunTimes import RunTimes
rt = RunTimes("Store")

最佳答案

我遇到了同样的问题,结果发现这个错误是因为我从 Base 继承的类与 python 文件和定义它的 python 包同名。 这里我从你的test.py可以看出也是这样的。尝试为包、文件和类使用不同的名称。

关于python - SQLAlchemy - 某些表类的 ModuleMarker 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53617203/

相关文章:

node.js - 在 KNEX/BookshelfJS 中定义 'pool' 的语法

python - SQLAlchemy 查找错误 : "DIVERSE" is not among the defined enum values

c++ - 为什么 python 的 dict 实现为哈希表,而 std::map 是基于树的?

python - 将字符串转换为numpy.ndarray python

python - 在接下来的 10 个字符中查找 python 中的各种字符串重复

python - 如何转置 3D 矩阵?

function - 将表名和列名定义为 plpgsql 函数中的参数?

postgresql - 自动递增,但忽略列中的现有值

python - 使用现有的 psycopg2 连接池创建一个 sqlalchemy 引擎

mysql - SQL 检查并正确时,sqlalchemy 不支持格式字符