cliche.orm
— Object-relational mapping¶
Cliche uses the relational database and data on the database are mapped to objects. It widely uses SQLAlchemy as its ORM (object-relational mapping) framework.
In order to define a persist model class, just subclass Base
:
from sqlalchemy import Column, Integer, UnicodeText
from .orm import Base
class Thing(Base):
'''A something object-relationally mapped.'''
id = Column(Integer, primary_key=True)
value = Column(UnicodeText, nullable=False)
__repr_columns = id, value
__tablename__ = 'things'
-
class
cliche.orm.
Base
(**kwargs)¶ SQLAlchemy declarative base class.
-
__repr_columns__
¶ (
collections.abc.Sequence
) This columns will be printed torepr()
string of its instances if__repr_columns__
is defined.
See also
- SQLAlchemy — Declarative
- Declarative allows all three to be expressed at once within the class declaration.
-
-
cliche.orm.
Session
= sessionmaker(class_='Session',bind=None, expire_on_commit=True, autoflush=True, autocommit=True)¶ SQLAlchemy session class.
See also
- SQLAlchemy — Using the Session
Session
is the primary usage interface for persistence operations.
-
cliche.orm.
downgrade_database
(engine, revision)¶ Reverts to a previous
revision
.Parameters: - engine (
sqlalchemy.engine.base.Engine
) – the database engine to revert - revision (
str
) – the previous revision to revert to
- engine (
-
cliche.orm.
get_alembic_config
(engine)¶ Creates a configuration for
alembic
. You can pass anEngine
object or a string of database url either. So:from sqlalchemy import create_engine engine = create_engine('postgresql://localhost/cliche') alembic_cfg = get_alembic_config(engine)
is equivalent to:
db_url = 'postgresql://localhost/cliche' alembic_cfg = get_alembic_config(db_url)
Parameters: engine ( sqlalchemy.engine.base.Engine
,str
) – the database engine to useReturns: an alembic config Return type: alembic.config.Config
-
cliche.orm.
get_database_revision
(engine)¶ Gets the current revision of the database.
Parameters: engine ( sqlalchemy.engine.base.Engine
) – the database engine to get the current revisionReturns: the script of the current revision Return type: alembic.script.Script
-
cliche.orm.
import_all_modules
(dry_run=False)¶ Import all submodules of
cliche
to ensure every ORM entity classes are ready to use. It’s useful for being ready to auto-generate a migration script.Returns: the set of module names Return type: collections.abc.Set
-
cliche.orm.
initialize_database
(engine)¶ Creates all database schemas and stamps it as the head of versions.
Parameters: engine ( sqlalchemy.engine.base.Engine
) – the database engine to initialize