orm

This module complements the sqlalchemy orm module, it contains the global Session factory to create session objects. Whenever a session is needed it can be constructed with a call of Session

session = Session

when using Elixir, Elixir needs to be told to use this session factory

elixir.session = Session

when using Declarative, this module contains an Entity class that can be used as a declarative_base and has some classes that mimic Elixir behavior

class camelot.core.orm.EntityBase(*args, **kwargs)

A declarative base class that adds some methods that used to be available in Elixir

from_dict(data)

Update a mapped class with data from a JSON-style nested dict/list structure.

classmethod get(*args, **kwargs)

Return the instance of this class based on the given identifier, or None if not found. This is equivalent to: session.query(MyClass).get(...)

classmethod get_by(*args, **kwargs)

Returns the first instance of this class matching the given criteria. This is equivalent to: session.query(MyClass).filter_by(...).first()

to_dict(deep={}, exclude=[])

Generate a JSON-style nested dict/list structure from an object.

class camelot.core.orm.EntityMeta(classname, bases, dict_)

Subclass of sqlalchmey.ext.declarative.DeclarativeMeta. This metaclass processes the Property and ClassMutator objects.

class camelot.core.orm.Field(type, *args, **kwargs)

Represents the definition of a ‘field’ on an entity.

This class represents a column on the table where the entity is stored.

class camelot.core.orm.GenericProperty(prop, *args, **kwargs)

Generic catch-all class to wrap an SQLAlchemy property.

class OrderLine(Entity):
    quantity = Field(Float)
    unit_price = Field(Numeric)
    price = GenericProperty(lambda c: column_property(
                     (c.quantity * c.unit_price).label('price')))
class camelot.core.orm.ColumnProperty(prop, *args, **kwargs)

A specialized form of the GenericProperty to generate SQLAlchemy column_property‘s.

It takes a function (often given as an anonymous lambda) as its first argument. Other arguments and keyword arguments are forwarded to the column_property construct. That first-argument function must accept exactly one argument and must return the desired (scalar-returning) SQLAlchemy ClauseElement.

The function will be called whenever the entity table is completely defined, and will be given the .c attribute of the table of the entity as argument (as a way to access the entity columns). The ColumnProperty will first wrap your ClauseElement in an “empty” label (ie it will be labelled automatically during queries), then wrap that in a column_property.

class OrderLine(Entity):
    quantity = Field(Float)
    unit_price = Field(Numeric)
    price = ColumnProperty(lambda c: c.quantity * c.unit_price,
                           deferred=True)

Please look at the corresponding SQLAlchemy documentation for details.

class camelot.core.orm.ManyToOne(of_kind, column_kwargs=None, colname=None, required=None, primary_key=None, field=None, constraint_kwargs=None, use_alter=None, ondelete=None, onupdate=None, target_column=None, *args, **kwargs)

Generates a many to one relationship.

create_keys(pk)

Find all primary keys on the target and create foreign keys on the source accordingly.

class camelot.core.orm.OneToMany(of_kind, filter=None, *args, **kwargs)

Generates a one to many relationship.

class camelot.core.orm.using_options(*args, **kwargs)

This statement its sole reason of existence is to keep existing Elixir model definitions working. Do not use it when writing new code, instead use Declarative directly.

camelot.core.orm.setup_all(create_tables=False, *args, **kwargs)[source]

Create all tables that are registered in the metadata

camelot.core.orm.transaction(original_function)[source]

Decorator to make methods transactional with regard to the session of the object on which they are called

Previous topic

memento

Next topic

entity

This Page