简体   繁体   English

无法在Postgresql sqlalchemy中创建表

[英]Unable to create tables in the postgresql sqlalchemy

I am trying to create a few tables in sqlalchemy database using Python. 我正在尝试使用Python在sqlalchemy数据库中创建一些表。 The tables are not getting created in the database and I can't find anything in the logging information. 在数据库中未创建表,并且在日志记录信息中找不到任何内容。

Model.py 模型

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

Department.py [I have more models and all of them imports the Model.py, but I can't see any of these tables in my database] 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 This class creates the engine. data_service_provider.py此类创建引擎。 I am calling this class from the below script. 我从下面的脚本中调用此类。

from models.InitDB import init_database from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from models.Model import Model import logging 从模型导入.initDB从sqlalchemy导入init_database从sqlalchemy导入create_engine.orm从模型导入sessionmaker.Model导入模型导入日志记录

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)

middleware.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 Settings.py

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

So basically Model.py is the base class which gets imported by all my model classes. 所以基本上Model.py是所有我的模型类都导入的基类。 In the data_service_provider.py I am creating the database engine and creating the tables. data_service_provider.py中,我正在创建数据库引擎并创建表。 The middleware.py scripts make the database URL from Settings.py and call the DataProviderService. middleware.py脚本从Settings.py创建数据库URL,然后调用DataProviderService。

I am trying for last few hours but the tables are not getting created. 我尝试了最后几个小时,但未创建表。 I can't find any useful information in the debug logs. 我在调试日志中找不到任何有用的信息。

Following are the debug logs: 以下是调试日志:

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:{}

I moved all my models into a different package but missed to add init.py which was causing issues. 我将所有模型移到了不同​​的程序包中,但是没有添加导致问题的init.py。 Adding init.py and specifying the model names into that resolved it. 添加init.py并指定模型名称即可解决该问题。 Thanks! 谢谢!

init.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM