gpt4 book ai didi

Flask-SQLAlchemy __table__ AttributeError for Only One Model in Multi-Model Setup(多模型设置中只有一个模型的FASK-SQLAlChemy__TABLE__AttributeError)

翻译 作者:bug小助手 更新时间:2023-10-26 22:26:26 25 4
gpt4 key购买 nike



I need some assistance with a Flask and SQLAlchemy setup I have. I'm loading my models in the following manner:

我需要一些关于烧瓶和SQLAlChemy设置的帮助。我以以下方式加载我的模型:


from models import User
from models import Project, ProjectForm
from models import Prompt, PromptForm
from models import Transaction
from models import Notification
from models import imageGeneration
from models import Package

Here's what my models/__init__.py file looks like:

下面是我的models/__init__.py文件的样子:


# models/__init__.py

from .User import User
from .imageGeneration import imageGeneration
from .Prompt import Prompt
from .PromptForm import PromptForm
from .Project import Project
from .ProjectForm import ProjectForm
from .Transaction import Transaction
from .Notification import Notification
from .Package import Package

During app initialization, when creating the database, I load all models like this:

在应用初始化期间,当创建数据库时,我像这样加载所有模型:


if __name__ == "__main__":
logger.warning('SqlAlchemy: Creating tables')
with app.app_context():
User.__table__.create(bind=db.engine, checkfirst=True)
Project.__table__.create(bind=db.engine, checkfirst=True)
Prompt.__table__.create(bind=db.engine, checkfirst=True)
Transaction.__table__.create(bind=db.engine, checkfirst=True)
Notification.__table__.create(bind=db.engine, checkfirst=True)
imageGeneration.__table__.create(bind=db.engine, checkfirst=True)
Package.__table__.create(bind=db.engine, checkfirst=True)
### here I'm starting the app

However, I'm encountering an error specifically with the Package model:
AttributeError: module 'models.Package' has no attribute '__table__'. Did you mean: '__file__'?

但是,我遇到了一个与包模型相关的错误:AttributeError:模块‘Models.Package’没有属性‘__TABLE__’。你的意思是:‘__文件__’?


The definition for my Package model is:

我的程序包模型的定义是:


from app import db
import uuid


class Package(db.Model):
__tablename__ = 'packages'

id = db.Column(db.Integer, primary_key=True)
uuid = db.Column(db.String(36), default=lambda: str(uuid.uuid4()),
unique=True, nullable=False)
name = db.Column(db.String(225), nullable=False)
price = db.Column(db.FLOAT(precision=10, decimal_return_scale=2))

Any ideas what might be wrong with my code?

你知道我的代码可能出了什么问题吗?


All the other models load fine. I'm only experiencing issues with this particular model. I've tried renaming it and even printed the content of the file in case Docker wasn't copying it correctly. I'm just excepting the package table to be created.

其他型号都能正常加载。我只在这个型号上遇到问题。我试着重命名它,甚至打印文件的内容,以防Docker没有正确复制它。我只是在等待要创建的包表。


更多回答

I'd advice following naming convention for packages and modules: all-lowercase, with possible underscores. It would also make it impossible for a module to shadow a variable. As for the problem, don't you have from . import * somewhere?

我建议遵循包和模块的命名约定:全小写,可能带有下划线。它还将使模块不可能隐藏变量。至于这个问题,你不知道吗?进口*在什么地方?

No, I never use that as a best practice.

不,我从来没有把它作为最佳实践。

I don't see how any of this can be working, as __table__ isn't created until the ORM class is mapped. Are you sure that db.create_all() isn't being called somewhere? (As a matter of interest, why not just use db.create_all?)

我看不出这是如何工作的,因为直到ORM类被映射后,__table__才被创建。你确定没有在某处调用db.create_all()吗?(As为什么不直接使用db.create_all呢?

It's actually not working for me and not creating the tables at all.

它实际上对我不起作用,根本不创建表。

I would remove all the Model.__table__.create stuff and replace with db.create_all() as documented here.

我将删除所有的Model.__TABLE__.Create内容,并替换为这里所述的db.create_all()。

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com