Skip to content

Instantly share code, notes, and snippets.

@davidjb
Last active July 13, 2021 10:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjb/9107107 to your computer and use it in GitHub Desktop.
Save davidjb/9107107 to your computer and use it in GitHub Desktop.
Example SQLAlchemy integration with Plone
[buildout]
...
[instance]
...
eggs=
...
z3c.saconfig
zcml-additional =
...
<configure xmlns="http://namespaces.zope.org/zope"
xmlns:db="http://namespaces.zope.org/db">
<include package="z3c.saconfig" file="meta.zcml" />
<db:engine name="my_engine_name"
url="mysql://david:password@localhost/my_db_name"
setup="my.example.prepare_model.prepare"
/>
<db:session name="session" engine="my_engine_name" />
</configure>
def prepare(engine):
# ``Base`` is a declarative_base object used for ORM classes.
from my.example import Base
# Need to import models so Base.metadata is aware of their existence
import my.example.models
# Binds the SQLAlchemy engine to the models' metadata so they can be accessed
Base.metadata.create_all(engine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment