Reputation: 727
I am trying to create a few tables in sqlalchemy database using Python. 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]
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. 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
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
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. In the data_service_provider.py I am creating the database engine and creating the tables. The middleware.py scripts make the database URL from Settings.py and call the 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:{}
Upvotes: 0
Views: 950
Reputation: 727
I moved all my models into a different package but missed to add init.py which was causing issues. Adding init.py and specifying the model names into that resolved it. 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
Upvotes: 0