Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeping form together with models is breaking SQLAlchemy on relationships #94

Open
hyperknot opened this issue Jan 28, 2016 · 0 comments

Comments

@hyperknot
Copy link

Basically, having model definitions like this:

init.py

BaseModelForm = model_form_factory(ModelForm_)


class ModelForm(BaseModelForm):
    class Meta:
        include_primary_keys = True
        include_foreign_keys = True

    @classmethod
    def get_session(self): # noqa
        return DBSession

parent.py

from sqlalchemy import Column, Integer
from sqlalchemy.orm import relationship

from . import Base, ModelForm

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child", back_populates="parent")

class MapForm(ModelForm):
    class Meta:
        model = Parent

child.py

from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship

from . import Base


class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent", back_populates="children")
Status API Training Shop Blog About Pricing

It breaks initializing with the following error message:

  File "/Users/user/.virtualenvs/temp/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1601, in mapper
    argument = self.argument()
  File "/Users/user/.virtualenvs/temp/lib/python2.7/site-packages/sqlalchemy/ext/declarative/clsregistry.py", line 293, in __call__
    (self.prop.parent, self.arg, n.args[0], self.cls)
sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper|Parent|parent, expression 'Child' failed to locate a name ("name 'Child' is not defined"). If this is a class name, consider adding this relationship() to the <class 'pyramidsqlalchemyrelationshipbug.models.parent.Parent'> class after both dependent classes have been defined.

I made a repo out of a minimal project:
https://github.com/hyperknot/pyramid-sqlalchemy-wtforms-relationship-bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant