mercredi 13 janvier 2016

Combining PyQt4 with SQLAlchemy

I am using an Object Relational Mapper (ORM) in SQLAlchemy 1.0.9, as backbone for an application interface written in PyQt4. My goal is to use the PyQt emit/signal/slot system (ref 1) to communicate changes in the model objects to the interface. However, this requires that the ORM-classes are derived from the QObject class. I think I found a way to do this: use the pyqtWrapperType metaclass and pass it to constructor declarative_base (ref 2), such that Base is derived from it.

My problem: when running init_db.py (see below), no tables are created in the database, even though the code doesn't give any errors. Yet when I run this code without using pyqtWrapperType as metaclass, it works just fine. I am now stuck with two questions: (1) what am I doing wrong and (2) how could I resolve this? I hope you have some suggestions for me.

projectmodels.py:

from sqlalchemy.ext.declarative   import declarative_base
from PyQt4.QtCore                 import QObject, pyqtWrapperType

Base   = declarative_base(metaclass = pyqtWrapperType)

class Project(Base):
    __tablename__ = 'Projects'

    ProjectID       = Column(Integer, primary_key=True)
    Name            = Column(String(250), nullable=False)
    YearZero        = Column(Integer)

init_db.py:

import projectmodels
import sqlalchemy as sqa

engine = sqa.create_engine('sqlite:///' + some_filepath_to_sqlite_db, echo = True)               
projectmodels.Base.metadata.create_all(engine)  

Aucun commentaire:

Enregistrer un commentaire