Skip to content

SQLAlchemy models with custom constructor are broken #7

@martijnvermaat

Description

@martijnvermaat

This is actually the same problem addressed by #2, but since that solution was not accepted I report the problem here without proposing a fix.

SQLAlchemy declarative model definitions with custom constructors cannot be used with fixture, since no arguments are passed to the constructor.

As an example, consider the first model in the SQLAlchemy ORM tutorial.

Below is example code using that model. It works if you remove the constructor from the model (in this example that doesn't matter), but it is a standard pattern when using SQLAlchemy so I thing this is a bug.

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from fixture import DataSet, SQLAlchemyFixture

engine = create_engine('sqlite:///:memory:')

Session = sessionmaker(bind=engine)
Base = declarative_base()

# http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#declare-a-mapping
class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    password = Column(String)

    def __init__(self, name, fullname, password):
        self.name = name
        self.fullname = fullname
        self.password = password

    def __repr__(self):
       return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)

Base.metadata.create_all(engine)

dbfixture = SQLAlchemyFixture(env={'UserData': User}, engine=engine)

class UserData(DataSet):
    class frank_herbert:
        name = 'Frank'
        fullname = 'Frank Herbert'
        password = 'password'

data = dbfixture.data(UserData)
data.setup()

session = Session()
print session.query(User).first().name

If you really feel there is no problem here, I'll of course leave it at that. Thanks for considering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions