gpt4 book ai didi

python - 无法在 postgresql sqlalchemy 中创建表

转载 作者:行者123 更新时间:2023-11-29 17:56:29 29 4
gpt4 key购买 nike

我正在尝试使用 Python 在 sqlalchemy 数据库中创建一些表。这些表没有在数据库中创建,我在日志信息中找不到任何内容。

模型.py

from sqlalchemy.ext.declarative import declarative_base
Model = declarative_base()

Department.py [我有更多模型,并且所有模型都导入 Model.py,但我在数据库中看不到任何这些表]

from sqlalchemy import Column, Integer, String
from models.Model import Model

class Department(Model):
__tablename__ = "department"
oid = Column('oid', Integer, primary_key=True)
name = Column('name', String, unique=True)

data_service_provider.py 此类创建引擎。我从下面的脚本中调用此类。

从 models.InitDB 导入 init_database从 sqlalchemy 导入 create_engine从 sqlalchemy.orm 导入 sessionmaker从模型.模型导入模型导入日志记录

class DataProviderService():
def __init__(self, engine):
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)
if not engine:
raise ValueError('The values specified in engine is not supported')
self.engine = engine
db_engine = create_engine(engine, echo=True)
Model.metadata.create_all(db_engine)

中间件.py

from sqlalchemy.engine.url import URL
from config import settings
from data_provider_service import DataProviderService

db_engine = URL(**settings.DATABASE);

DATA_PROVIDER = DataProviderService(db_engine)

Settings.py

DATABASE = {
'drivername': 'postgresql',
'host': 'localhost',
'port': '5432',
'username': 'postgres',
'password': 'punitjain',
'database': 'csuf'
}

所以基本上 Model.py 是由我的所有模型类导入的基类。在data_service_provider.py中,我正在创建数据库引擎并创建表。 middleware.py 脚本从 Settings.py 生成数据库 URL 并调用 DataProviderService。

我在过去的几个小时里一直在尝试,但表没有被创建。我在调试日志中找不到任何有用的信息。

以下是调试日志:

postgresql://postgres:password@localhost:5432/csuf
2018-02-13 04:17:55,496 INFO sqlalchemy.engine.base.Engine select version()
2018-02-13 04:17:55,496 INFO sqlalchemy.engine.base.Engine {}
2018-02-13 04:17:55,497 INFO sqlalchemy.engine.base.Engine select
current_schema()
2018-02-13 04:17:55,497 INFO sqlalchemy.engine.base.Engine {}
2018-02-13 04:17:55,498 INFO sqlalchemy.engine.base.Engine SELECT CAST('test
plain returns' AS VARCHAR(60)) AS anon_1
2018-02-13 04:17:55,499 INFO sqlalchemy.engine.base.Engine {}
INFO:sqlalchemy.engine.base.Engine:select version()
2018-02-13 04:17:55,499 INFO sqlalchemy.engine.base.Engine SELECT CAST('test
unicode returns' AS VARCHAR(60)) AS anon_1
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:select current_schema()
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:SELECT CAST('test plain returns' AS
VARCHAR(60)) AS anon_1
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:SELECT CAST('test unicode returns' AS
VARCHAR(60)) AS anon_1
2018-02-13 04:17:55,500 INFO sqlalchemy.engine.base.Engine {}
INFO:sqlalchemy.engine.base.Engine:{}
2018-02-13 04:17:55,500 INFO sqlalchemy.engine.base.Engine show
standard_conforming_strings
INFO:sqlalchemy.engine.base.Engine:show standard_conforming_strings
2018-02-13 04:17:55,500 INFO sqlalchemy.engine.base.Engine {}
INFO:sqlalchemy.engine.base.Engine:{}

最佳答案

我将所有模型移至不同的包中,但错过了添加导致问题的 init.py。添加 init.py 并指定模型名称即可解决该问题。谢谢!

初始化.py

__all__ = ["College", "Course", "Department", "Program", "init_database"]

from models.College import College
from models.Course import Course
from models.Department import Department
from models.Program import Program

关于python - 无法在 postgresql sqlalchemy 中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48766720/

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