pythonpython-3.xsqlalchemypyramidcolanderalchemy

Where to put automatic setup_schema for ColanderAlchemy+Pyramid


The docs say to put this somewhere:

from sqlalchemy import event
from colanderalchemy import setup_schema
event.listen(mapper, 'mapper_configured', setup_schema)

Where should this go in Pyramid? Should I be using Pyramid events instead of SQLAlchemy's?

When I tried putting it at the top of the models.py file, it complained about mapper not existing; should I still be using that?


Solution

  • You need to use the SQLAlchemy events as they tell what is happening inside the SQLAlchemy (they do not relate to the pyramid events at all).

    The documentation for the ColanderAlchemy is confusing; what they call for a mapper here is your model class (it is not a mapper).

    Thus in your models you should be doing something like:

    class MyModelClass(Base):
        ...
    
    event.listen(
        MyModelClass,
        "mapper_configured",
        setup_schema)