SQLAlchemy-某些表类上的ModuleMarker错误 [英] SQLAlchemy - ModuleMarker Errror on certain table classes

查看:180
本文介绍了SQLAlchemy-某些表类上的ModuleMarker错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple class that accesses a table for storing run times. However when I try to import the class I get the following error.

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

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

I have been able to determine it is something to do with setting up the table name and columns as I have 2 other classes accessing two different tables and it is working fine there. I basically copy pasted the code from the 2 original classes so I am not sure why it doesn't work.

有没有办法我可以生成一个更有用的错误代码?

Is there a way I can produce a more helpful error code?

运行时间类

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()

test.py

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


推荐答案

我遇到了同样的问题,结果是出现了此错误,因为从 Base 继承的类与python文件具有相同的名称,和定义它的python包。
在这里,我从您的 test.py 可以看到这种情况。尝试为包,文件和类使用不同的名称。

I had the same issue and it turned out that this error arose because my class that inherited from Base had the same name as the python file, and python package where it was defined. Here I can see from your test.py that it's also the case. Try having different names for the package, the file and the class.

这篇关于SQLAlchemy-某些表类上的ModuleMarker错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆