SQLAlchemy 0.6.1 Documentation

Version: 0.6.1 Last Updated: 07/25/2016 21:14:41
API Reference | Index

Class Mapping

Defining Mappings

Python classes are mapped to the database using the mapper() function.

sqlalchemy.orm.mapper(class_, local_table=None, *args, **params)

Return a new Mapper object.

Parameters:
  • class_ – The class to be mapped.
  • local_table – The table to which the class is mapped, or None if this mapper inherits from another mapper using concrete table inheritance.
  • always_refresh – If True, all query operations for this mapped class will overwrite all data within object instances that already exist within the session, erasing any in-memory changes with whatever information was loaded from the database. Usage of this flag is highly discouraged; as an alternative, see the method populate_existing() on Query.
  • allow_null_pks – This flag is deprecated - this is stated as allow_partial_pks which defaults to True.
  • allow_partial_pks – Defaults to True. Indicates that a composite primary key with some NULL values should be considered as possibly existing within the database. This affects whether a mapper will assign an incoming row to an existing identity, as well as if session.merge() will check the database first for a particular primary key value. A “partial primary key” can occur if one has mapped to an OUTER JOIN, for example.
  • batch – Indicates that save operations of multiple entities can be batched together for efficiency. setting to False indicates that an instance will be fully saved before saving the next instance, which includes inserting/updating all table rows corresponding to the entity as well as calling all MapperExtension methods corresponding to the save operation.
  • column_prefix – A string which will be prepended to the key name of all Columns when creating column-based properties from the given Table. Does not affect explicitly specified column-based properties
  • concrete – If True, indicates this mapper should use concrete table inheritance with its parent mapper.
  • exclude_properties – A list of properties not to map. Columns present in the mapped table and present in this list will not be automatically converted into properties. Note that neither this option nor include_properties will allow an end-run around Python inheritance. If mapped class B inherits from mapped class A, no combination of includes or excludes will allow B to have fewer properties than its superclass, A.
  • extension – A MapperExtension instance or list of MapperExtension instances which will be applied to all operations by this Mapper.
  • include_properties – An inclusive list of properties to map. Columns present in the mapped table but not present in this list will not be automatically converted into properties.
  • inherits – Another Mapper for which this Mapper will have an inheritance relationship with.
  • inherit_condition – For joined table inheritance, a SQL expression (constructed ClauseElement) which will define how the two tables are joined; defaults to a natural join between the two tables.
  • inherit_foreign_keys – When inherit_condition is used and the condition contains no ForeignKey columns, specify the “foreign” columns of the join condition in this list. else leave as None.
  • non_primary – Construct a Mapper that will define only the selection of instances, not their persistence. Any number of non_primary mappers may be created for a particular class.
  • order_by – A single Column or list of Columns for which selection operations should use as the default ordering for entities. Defaults to the OID/ROWID of the table if any, or the first primary key column of the table.
  • passive_updates

    Indicates UPDATE behavior of foreign keys when a primary key changes on a joined-table inheritance or other joined table mapping.

    When True, it is assumed that ON UPDATE CASCADE is configured on the foreign key in the database, and that the database will handle propagation of an UPDATE from a source column to dependent rows. Note that with databases which enforce referential integrity (i.e. PostgreSQL, MySQL with InnoDB tables), ON UPDATE CASCADE is required for this operation. The relationship() will update the value of the attribute on related items which are locally present in the session during a flush.

    When False, it is assumed that the database does not enforce referential integrity and will not be issuing its own CASCADE operation for an update. The relationship() will issue the appropriate UPDATE statements to the database in response to the change of a referenced key, and items locally present in the session during a flush will also be refreshed.

    This flag should probably be set to False if primary key changes are expected and the database in use doesn’t support CASCADE (i.e. SQLite, MySQL MyISAM tables).

    Also see the passive_updates flag on relationship().

    A future SQLAlchemy release will provide a “detect” feature for this flag.

  • polymorphic_on – Used with mappers in an inheritance relationship, a Column which will identify the class/mapper combination to be used with a particular row. Requires the polymorphic_identity value to be set for all mappers in the inheritance hierarchy. The column specified by polymorphic_on is usually a column that resides directly within the base mapper’s mapped table; alternatively, it may be a column that is only present within the <selectable> portion of the with_polymorphic argument.
  • polymorphic_identity – A value which will be stored in the Column denoted by polymorphic_on, corresponding to the class identity of this mapper.
  • properties – A dictionary mapping the string names of object attributes to MapperProperty instances, which define the persistence behavior of that attribute. Note that the columns in the mapped table are automatically converted into ColumnProperty instances based on the key property of each Column (although they can be overridden using this dictionary).
  • primary_key – A list of Column objects which define the primary key to be used against this mapper’s selectable unit. This is normally simply the primary key of the local_table, but can be overridden here.
  • version_id_col – A Column which must have an integer type that will be used to keep a running version id of mapped entities in the database. this is used during save operations to ensure that no other thread or process has updated the instance during the lifetime of the entity, else a ConcurrentModificationError exception is thrown.
  • version_id_generator

    A callable which defines the algorithm used to generate new version ids. Defaults to an integer generator. Can be replaced with one that generates timestamps, uuids, etc. e.g.:

    import uuid
    
    mapper(Cls, table, 
    version_id_col=table.c.version_uuid,
    version_id_generator=lambda version:uuid.uuid4().hex
    )

    The callable receives the current version identifier as its single argument.

  • with_polymorphic – A tuple in the form (<classes>, <selectable>) indicating the default style of “polymorphic” loading, that is, which tables are queried at once. <classes> is any single or list of mappers and/or classes indicating the inherited classes that should be loaded at once. The special value '*' may be used to indicate all descending classes should be loaded immediately. The second tuple argument <selectable> indicates a selectable that will be used to query for multiple classes. Normally, it is left as None, in which case this mapper will form an outer join from the base mapper’s table to that of all desired sub-mappers. When specified, it provides the selectable to be used for polymorphic loading. When with_polymorphic includes mappers which load from a “concrete” inheriting table, the <selectable> argument is required, since it usually requires more complex UNION queries.

Mapper Properties

A basic mapping of a class will simply make the columns of the database table or selectable available as attributes on the class. Mapper properties allow you to customize and add additional properties to your classes, for example making the results one-to-many join available as a Python list of related objects.

Mapper properties are most commonly included in the mapper() call:

mapper(Parent, properties={
   'children': relationship(Children)
}
sqlalchemy.orm.backref(name, **kwargs)

Create a back reference with explicit arguments, which are the same arguments one can send to relationship().

Used with the backref keyword argument to relationship() in place of a string argument.

sqlalchemy.orm.column_property(*args, **kwargs)

Provide a column-level property for use with a Mapper.

Column-based properties can normally be applied to the mapper’s properties dictionary using the schema.Column element directly. Use this function when the given column is not directly present within the mapper’s selectable; examples include SQL expressions, functions, and scalar SELECT queries.

Columns that aren’t present in the mapper’s selectable won’t be persisted by the mapper and are effectively “read-only” attributes.

*cols
list of Column objects to be mapped.
comparator_factory
a class which extends sqlalchemy.orm.properties.ColumnProperty.Comparator which provides custom SQL clause generation for comparison operations.
group
a group name for this property when marked as deferred.
deferred
when True, the column property is “deferred”, meaning that it does not load immediately, and is instead loaded when the attribute is first accessed on an instance. See also deferred().
doc
optional string that will be applied as the doc on the class-bound descriptor.
extension
an AttributeExtension instance, or list of extensions, which will be prepended to the list of attribute listeners for the resulting descriptor placed on the class. These listeners will receive append and set events before the operation proceeds, and may be used to halt (via exception throw) or change the value used in the operation.
sqlalchemy.orm.comparable_property(comparator_factory, descriptor=None)

Provide query semantics for an unmanaged attribute.

Allows a regular Python @property (descriptor) to be used in Queries and SQL constructs like a managed attribute. comparable_property wraps a descriptor with a proxy that directs operator overrides such as == (__eq__) to the supplied comparator but proxies everything else through to the original descriptor:

class MyClass(object):
    @property
    def myprop(self):
        return 'foo'

class MyComparator(sqlalchemy.orm.interfaces.PropComparator):
    def __eq__(self, other):
        ....

mapper(MyClass, mytable, properties=dict(
         'myprop': comparable_property(MyComparator)))

Used with the properties dictionary sent to mapper().

comparator_factory
A PropComparator subclass or factory that defines operator behavior for this property.
descriptor

Optional when used in a properties={} declaration. The Python descriptor or property to layer comparison behavior on top of.

The like-named descriptor will be automatically retreived from the mapped class if left blank in a properties declaration.

sqlalchemy.orm.composite(class_, *cols, **kwargs)

Return a composite column-based property for use with a Mapper.

This is very much like a column-based property except the given class is used to represent “composite” values composed of one or more columns.

The class must implement a constructor with positional arguments matching the order of columns supplied here, as well as a __composite_values__() method which returns values in the same order.

A simple example is representing separate two columns in a table as a single, first-class “Point” object:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __composite_values__(self):
        return self.x, self.y
    def __eq__(self, other):
        return other is not None and self.x == other.x and self.y == other.y

# and then in the mapping:
... composite(Point, mytable.c.x, mytable.c.y) ...

The composite object may have its attributes populated based on the names of the mapped columns. To override the way internal state is set, additionally implement __set_composite_values__:

class Point(object):
    def __init__(self, x, y):
        self.some_x = x
        self.some_y = y
    def __composite_values__(self):
        return self.some_x, self.some_y
    def __set_composite_values__(self, x, y):
        self.some_x = x
        self.some_y = y
    def __eq__(self, other):
        return other is not None and self.some_x == other.x and self.some_y == other.y

Arguments are:

class_
The “composite type” class.
*cols
List of Column objects to be mapped.
group
A group name for this property when marked as deferred.
deferred
When True, the column property is “deferred”, meaning that it does not load immediately, and is instead loaded when the attribute is first accessed on an instance. See also deferred().
comparator_factory
a class which extends sqlalchemy.orm.properties.CompositeProperty.Comparator which provides custom SQL clause generation for comparison operations.
doc
optional string that will be applied as the doc on the class-bound descriptor.
extension
an AttributeExtension instance, or list of extensions, which will be prepended to the list of attribute listeners for the resulting descriptor placed on the class. These listeners will receive append and set events before the operation proceeds, and may be used to halt (via exception throw) or change the value used in the operation.
sqlalchemy.orm.deferred(*columns, **kwargs)

Return a DeferredColumnProperty, which indicates this object attributes should only be loaded from its corresponding table column when first accessed.

Used with the properties dictionary sent to mapper().

sqlalchemy.orm.dynamic_loader(argument, secondary=None, primaryjoin=None, secondaryjoin=None, foreign_keys=None, backref=None, post_update=False, cascade=False, remote_side=None, enable_typechecks=True, passive_deletes=False, doc=None, order_by=None, comparator_factory=None, query_class=None)

Construct a dynamically-loading mapper property.

This property is similar to relationship(), except read operations return an active Query object which reads from the database when accessed. Items may be appended to the attribute via append(), or removed via remove(); changes will be persisted to the database during a Sesion.flush(). However, no other Python list or collection mutation operations are available.

A subset of arguments available to relationship() are available here.

Parameters:
  • argument – a class or Mapper instance, representing the target of the relationship.
  • secondary – for a many-to-many relationship, specifies the intermediary table. The secondary keyword argument should generally only be used for a table that is not otherwise expressed in any class mapping. In particular, using the Association Object Pattern is generally mutually exclusive with the use of the secondary keyword argument.
  • query_class – Optional, a custom Query subclass to be used as the basis for dynamic collection.
sqlalchemy.orm.relation(*arg, **kw)
A synonym for relationship().
sqlalchemy.orm.relationship(argument, secondary=None, **kwargs)

Provide a relationship of a primary Mapper to a secondary Mapper.

Note

This function is known as relation() in all versions of SQLAlchemy prior to version 0.6beta2, including the 0.5 and 0.4 series. relationship() is only available starting with SQLAlchemy 0.6beta2. The relation() name will remain available for the foreseeable future in order to enable cross-compatibility.

This corresponds to a parent-child or associative table relationship. The constructed class is an instance of RelationshipProperty.

A typical relationship():

mapper(Parent, properties={
  'children': relationship(Children)
})
Parameters:
  • argument – a class or Mapper instance, representing the target of the relationship.
  • secondary – for a many-to-many relationship, specifies the intermediary table. The secondary keyword argument should generally only be used for a table that is not otherwise expressed in any class mapping. In particular, using the Association Object Pattern is generally mutually exclusive with the use of the secondary keyword argument.
  • backref – indicates the string name of a property to be placed on the related mapper’s class that will handle this relationship in the other direction. The other property will be created automatically when the mappers are configured. Can also be passed as a backref() object to control the configuration of the new relationship.
  • back_populates – Takes a string name and has the same meaning as backref, except the complementing property is not created automatically, and instead must be configured explicitly on the other mapper. The complementing property should also indicate back_populates to this relationship to ensure proper functioning.
  • cascade

    a comma-separated list of cascade rules which determines how Session operations should be “cascaded” from parent to child. This defaults to False, which means the default cascade should be used. The default value is "save-update, merge".

    Available cascades are:

    • save-update - cascade the add() operation. This cascade applies both to future and past calls to add(), meaning new items added to a collection or scalar relationship get placed into the same session as that of the parent, and also applies to items which have been removed from this relationship but are still part of unflushed history.
    • merge - cascade the merge() operation
    • expunge - cascade the expunge() operation
    • delete - cascade the delete() operation
    • delete-orphan - if an item of the child’s type with no parent is detected, mark it for deletion. Note that this option prevents a pending item of the child’s class from being persisted without a parent present.
    • refresh-expire - cascade the expire() and refresh() operations
    • all - shorthand for “save-update,merge, refresh-expire, expunge, delete”
  • collection_class – a class or callable that returns a new list-holding object. will be used in place of a plain list for storing elements.
  • comparator_factory – a class which extends RelationshipProperty.Comparator which provides custom SQL clause generation for comparison operations.
  • doc – docstring which will be applied to the resulting descriptor.
  • extension – an AttributeExtension instance, or list of extensions, which will be prepended to the list of attribute listeners for the resulting descriptor placed on the class. These listeners will receive append and set events before the operation proceeds, and may be used to halt (via exception throw) or change the value used in the operation.
  • foreign_keys – a list of columns which are to be used as “foreign key” columns. this parameter should be used in conjunction with explicit primaryjoin and secondaryjoin (if needed) arguments, and the columns within the foreign_keys list should be present within those join conditions. Normally, relationship() will inspect the columns within the join conditions to determine which columns are the “foreign key” columns, based on information in the Table metadata. Use this argument when no ForeignKey’s are present in the join condition, or to override the table-defined foreign keys.
  • innerjoin=False – when True, joined eager loads will use an inner join to join against related tables instead of an outer join. The purpose of this option is strictly one of performance, as inner joins generally perform better than outer joins. This flag can be set to True when the relationship references an object via many-to-one using local foreign keys that are not nullable, or when the reference is one-to-one or a collection that is guaranteed to have one or at least one entry.
  • join_depth – when non-None, an integer value indicating how many levels deep “eager” loaders should join on a self-referring or cyclical relationship. The number counts how many times the same Mapper shall be present in the loading condition along a particular join branch. When left at its default of None, eager loaders will stop chaining when they encounter a the same target mapper which is already higher up in the chain. This option applies both to joined- and subquery- eager loaders.
  • lazy=(‘select’|’joined’|’subquery’|’noload’|’dynamic’)

    specifies how the related items should be loaded. Values include:

    • ‘select’ - items should be loaded lazily when the property is first accessed.
    • ‘joined’ - items should be loaded “eagerly” in the same query as that of the parent, using a JOIN or LEFT OUTER JOIN.
    • ‘subquery’ - items should be loaded “eagerly” within the same query as that of the parent, using a second SQL statement which issues a JOIN to a subquery of the original statement.
    • ‘noload’ - no loading should occur at any time. This is to support “write-only” attributes, or attributes which are populated in some manner specific to the application.
    • ‘dynamic’ - the attribute will return a pre-configured Query object for all read operations, onto which further filtering operations can be applied before iterating the results. The dynamic collection supports a limited set of mutation operations, allowing append() and remove(). Changes to the collection will not be visible until flushed to the database, where it is then refetched upon iteration.
    • True - a synonym for ‘select’
    • False - a synonyn for ‘joined’
    • None - a synonym for ‘noload’
  • order_by – indicates the ordering that should be applied when loading these items.
  • passive_deletes=False

    Indicates loading behavior during delete operations.

    A value of True indicates that unloaded child items should not be loaded during a delete operation on the parent. Normally, when a parent item is deleted, all child items are loaded so that they can either be marked as deleted, or have their foreign key to the parent set to NULL. Marking this flag as True usually implies an ON DELETE <CASCADE|SET NULL> rule is in place which will handle updating/deleting child rows on the database side.

    Additionally, setting the flag to the string value ‘all’ will disable the “nulling out” of the child foreign keys, when there is no delete or delete-orphan cascade enabled. This is typically used when a triggering or error raise scenario is in place on the database side. Note that the foreign key attributes on in-session child objects will not be changed after a flush occurs so this is a very special use-case setting.

  • passive_updates=True

    Indicates loading and INSERT/UPDATE/DELETE behavior when the source of a foreign key value changes (i.e. an “on update” cascade), which are typically the primary key columns of the source row.

    When True, it is assumed that ON UPDATE CASCADE is configured on the foreign key in the database, and that the database will handle propagation of an UPDATE from a source column to dependent rows. Note that with databases which enforce referential integrity (i.e. PostgreSQL, MySQL with InnoDB tables), ON UPDATE CASCADE is required for this operation. The relationship() will update the value of the attribute on related items which are locally present in the session during a flush.

    When False, it is assumed that the database does not enforce referential integrity and will not be issuing its own CASCADE operation for an update. The relationship() will issue the appropriate UPDATE statements to the database in response to the change of a referenced key, and items locally present in the session during a flush will also be refreshed.

    This flag should probably be set to False if primary key changes are expected and the database in use doesn’t support CASCADE (i.e. SQLite, MySQL MyISAM tables).

    Also see the passive_updates flag on mapper().

    A future SQLAlchemy release will provide a “detect” feature for this flag.

  • post_update – this indicates that the relationship should be handled by a second UPDATE statement after an INSERT or before a DELETE. Currently, it also will issue an UPDATE after the instance was UPDATEd as well, although this technically should be improved. This flag is used to handle saving bi-directional dependencies between two individual rows (i.e. each row references the other), where it would otherwise be impossible to INSERT or DELETE both rows fully since one row exists before the other. Use this flag when a particular mapping arrangement will incur two rows that are dependent on each other, such as a table that has a one-to-many relationship to a set of child rows, and also has a column that references a single child row within that list (i.e. both tables contain a foreign key to each other). If a flush() operation returns an error that a “cyclical dependency” was detected, this is a cue that you might want to use post_update to “break” the cycle.
  • primaryjoin – a ColumnElement (i.e. WHERE criterion) that will be used as the primary join of this child object against the parent object, or in a many-to-many relationship the join of the primary object to the association table. By default, this value is computed based on the foreign key relationships of the parent and child tables (or association table).
  • remote_side – used for self-referential relationships, indicates the column or list of columns that form the “remote side” of the relationship.
  • secondaryjoin – a ColumnElement (i.e. WHERE criterion) that will be used as the join of an association table to the child object. By default, this value is computed based on the foreign key relationships of the association and child tables.
  • single_parent=(True|False) – when True, installs a validator which will prevent objects from being associated with more than one parent at a time. This is used for many-to-one or many-to-many relationships that should be treated either as one-to-one or one-to-many. Its usage is optional unless delete-orphan cascade is also set on this relationship(), in which case its required (new in 0.5.2).
  • uselist=(True|False) – a boolean that indicates if this property should be loaded as a list or a scalar. In most cases, this value is determined automatically by relationship(), based on the type and direction of the relationship - one to many forms a list, many to one forms a scalar, many to many is a list. If a scalar is desired where normally a list would be present, such as a bi-directional one-to-one relationship, set uselist to False.
  • viewonly=False – when set to True, the relationship is used only for loading objects within the relationship, and has no effect on the unit-of-work flush process. Relationships with viewonly can specify any kind of join conditions to provide additional views of related objects onto a parent object. Note that the functionality of a viewonly relationship has its limits - complicated join conditions may not compile into eager or lazy loaders properly. If this is the case, use an alternative method.
sqlalchemy.orm.synonym(name, map_column=False, descriptor=None, comparator_factory=None, doc=None)

Set up name as a synonym to another mapped property.

Used with the properties dictionary sent to mapper().

Any existing attributes on the class which map the key name sent to the properties dictionary will be used by the synonym to provide instance-attribute behavior (that is, any Python property object, provided by the property builtin or providing a __get__(), __set__() and __del__() method). If no name exists for the key, the synonym() creates a default getter/setter object automatically and applies it to the class.

name refers to the name of the existing mapped property, which can be any other MapperProperty including column-based properties and relationships.

If map_column is True, an additional ColumnProperty is created on the mapper automatically, using the synonym’s name as the keyname of the property, and the keyname of this synonym() as the name of the column to map. For example, if a table has a column named status:

class MyClass(object):
    def _get_status(self):
        return self._status
    def _set_status(self, value):
        self._status = value
    status = property(_get_status, _set_status)

mapper(MyClass, sometable, properties={
    "status":synonym("_status", map_column=True)
})

The column named status will be mapped to the attribute named _status, and the status attribute on MyClass will be used to proxy access to the column-based attribute.

Decorators

sqlalchemy.orm.reconstructor(fn)

Decorate a method as the ‘reconstructor’ hook.

Designates a method as the “reconstructor”, an __init__-like method that will be called by the ORM after the instance has been loaded from the database or otherwise reconstituted.

The reconstructor will be invoked with no arguments. Scalar (non-collection) database-mapped attributes of the instance will be available for use within the function. Eagerly-loaded collections are generally not yet available and will usually only contain the first element. ORM state changes made to objects at this stage will not be recorded for the next flush() operation, so the activity within a reconstructor should be conservative.

sqlalchemy.orm.validates(*names)

Decorate a method as a ‘validator’ for one or more named properties.

Designates a method as a validator, a method which receives the name of the attribute as well as a value to be assigned, or in the case of a collection to be added to the collection. The function can then raise validation exceptions to halt the process from continuing, or can modify or replace the value before proceeding. The function should otherwise return the given value.

Utilities

sqlalchemy.orm.object_mapper(instance)

Given an object, return the primary Mapper associated with the object instance.

Raises UnmappedInstanceError if no mapping is configured.

sqlalchemy.orm.class_mapper(class_, compile=True)

Given a class, return the primary Mapper associated with the key.

Raises UnmappedClassError if no mapping is configured.

sqlalchemy.orm.compile_mappers()

Compile all mappers that have been defined.

This is equivalent to calling compile() on any individual mapper.

sqlalchemy.orm.clear_mappers()

Remove all mappers that have been created thus far.

The mapped classes will return to their initial “unmapped” state and can be re-mapped with new mappers.

Attribute Utilities

sqlalchemy.orm.attributes.del_attribute(instance, key)

Delete the value of an attribute, firing history events.

This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to establish attribute state as understood by SQLAlchemy.

sqlalchemy.orm.attributes.get_attribute(instance, key)

Get the value of an attribute, firing any callables required.

This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to make usage of attribute state as understood by SQLAlchemy.

sqlalchemy.orm.attributes.get_history(obj, key, **kwargs)

Return a History record for the given object and attribute key.

obj is an instrumented object instance. An InstanceState is accepted directly for backwards compatibility but this usage is deprecated.

sqlalchemy.orm.attributes.init_collection(obj, key)

Initialize a collection attribute and return the collection adapter.

This function is used to provide direct access to collection internals for a previously unloaded attribute. e.g.:

collection_adapter = init_collection(someobject, 'elements')
for elem in values:
    collection_adapter.append_without_event(elem)

For an easier way to do the above, see set_committed_value().

obj is an instrumented object instance. An InstanceState is accepted directly for backwards compatibility but this usage is deprecated.

sqlalchemy.orm.attributes.instance_state()
Return the InstanceState for a given object.
sqlalchemy.orm.attributes.is_instrumented(instance, key)

Return True if the given attribute on the given instance is instrumented by the attributes package.

This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required.

sqlalchemy.orm.attributes.manager_of_class()
Return the ClassManager for a given class.
sqlalchemy.orm.attributes.set_attribute(instance, key, value)

Set the value of an attribute, firing history events.

This function may be used regardless of instrumentation applied directly to the class, i.e. no descriptors are required. Custom attribute management schemes will need to make usage of this method to establish attribute state as understood by SQLAlchemy.

sqlalchemy.orm.attributes.set_committed_value(instance, key, value)

Set the value of an attribute with no history events.

Cancels any previous history present. The value should be a scalar value for scalar-holding attributes, or an iterable for any collection-holding attribute.

This is the same underlying method used when a lazy loader fires off and loads additional data from the database. In particular, this method can be used by application code which has loaded additional attributes or collections through separate queries, which can then be attached to an instance as though it were part of its original loaded state.

Internals

class sqlalchemy.orm.mapper.Mapper(class_, local_table, properties=None, primary_key=None, non_primary=False, inherits=None, inherit_condition=None, inherit_foreign_keys=None, extension=None, order_by=False, always_refresh=False, version_id_col=None, version_id_generator=None, polymorphic_on=None, _polymorphic_map=None, polymorphic_identity=None, concrete=False, with_polymorphic=None, allow_null_pks=None, allow_partial_pks=True, batch=True, column_prefix=None, include_properties=None, exclude_properties=None, passive_updates=True, eager_defaults=False, _compiled_cache_size=100)

Define the correlation of class attributes to database table columns.

Instances of this class should be constructed via the mapper() function.

__init__(class_, local_table, properties=None, primary_key=None, non_primary=False, inherits=None, inherit_condition=None, inherit_foreign_keys=None, extension=None, order_by=False, always_refresh=False, version_id_col=None, version_id_generator=None, polymorphic_on=None, _polymorphic_map=None, polymorphic_identity=None, concrete=False, with_polymorphic=None, allow_null_pks=None, allow_partial_pks=True, batch=True, column_prefix=None, include_properties=None, exclude_properties=None, passive_updates=True, eager_defaults=False, _compiled_cache_size=100)

Construct a new mapper.

Mappers are normally constructed via the
mapper() function. See for details.
add_properties(dict_of_properties)
Add the given dictionary of properties to this mapper, using add_property.
add_property(key, prop)

Add an individual MapperProperty to this mapper.

If the mapper has not been compiled yet, just adds the property to the initial properties dictionary sent to the constructor. If this Mapper has already been compiled, then the given MapperProperty is compiled immediately.

cascade_iterator(type_, state, halt_on=None)

Iterate each element and its mapper in an object graph, for all relationships that meet the given cascade rule.

type\_:
The name of the cascade rule (i.e. save-update, delete, etc.)
state:
The lead InstanceState. child items will be processed per the relationships defined for this object’s mapper.

the return value are object instances; this provides a strong reference so that they don’t fall out of scope immediately.

common_parent(other)
Return true if the given mapper shares a common inherited parent as this mapper.
compile()

Compile this mapper and all other non-compiled mappers.

This method checks the local compiled status as well as for any new mappers that have been defined, and is safe to call repeatedly.

get_property(key, resolve_synonyms=False, raiseerr=True)
return a MapperProperty associated with the given key.
identity_key_from_instance(instance)

Return the identity key for the given instance, based on its primary key attributes.

This value is typically also found on the instance state under the attribute name key.

identity_key_from_primary_key(primary_key)

Return an identity-map key for use in storing/retrieving an item from an identity map.

primary_key
A list of values indicating the identifier.
identity_key_from_row(row, adapter=None)

Return an identity-map key for use in storing/retrieving an item from the identity map.

row
A sqlalchemy.engine.base.RowProxy instance or a dictionary corresponding result-set ColumnElement instances to their values within a row.
isa(other)
Return True if the this mapper inherits from the given mapper.
iterate_properties
return an iterator of all MapperProperty objects.
polymorphic_iterator()

Iterate through the collection including this mapper and all descendant mappers.

This includes not just the immediately inheriting mappers but all their inheriting mappers as well.

To iterate through an entire hierarchy, use mapper.base_mapper.polymorphic_iterator().

primary_key_from_instance(instance)
Return the list of primary key values for the given instance.
primary_mapper()
Return the primary mapper corresponding to this mapper’s class key (class).
Previous: sqlalchemy.orm Next: Collection Mapping