Ñò Š„[Jc@sBdZddklZlZlZddklZlZl Z l Z ddk l Z ddk lZlZddklZddklZlZddklZdZd „Zd„Zdefd„ƒYZdefd„ƒYZd„Zed„Z d„Z!d„Z"de"_#dddede"edd„Z%d„Z&dS(sï<A simple declarative layer for SQLAlchemy ORM. Synopsis ======== SQLAlchemy object-relational configuration involves the usage of :class:`~sqlalchemy.schema.Table`, :func:`~sqlalchemy.orm.mapper`, and class objects to define the three areas of configuration. ``declarative`` moves these three types of configuration underneath the individual mapped class. Regular SQLAlchemy schema and ORM constructs are used in most cases:: from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class SomeClass(Base): __tablename__ = 'some_table' id = Column(Integer, primary_key=True) name = Column(String(50)) Above, the :func:`declarative_base` callable produces a new base class from which all mapped classes inherit from. When the class definition is completed, a new :class:`~sqlalchemy.schema.Table` and :class:`~sqlalchemy.orm.mapper` have been generated, accessible via the ``__table__`` and ``__mapper__`` attributes on the ``SomeClass`` class. Defining Attributes =================== :class:`~sqlalchemy.schema.Column` objects may be explicitly named, including using a different name than the attribute in which they are associated. The column will be assigned to the :class:`~sqlalchemy.schema.Table` using the given name, and mapped to the class using the attribute name:: class SomeClass(Base): __tablename__ = 'some_table' id = Column("some_table_id", Integer, primary_key=True) name = Column("name", String(50)) Otherwise, you may omit the names from the Column definitions. Declarative will set the ``name`` attribute on the column when the class is initialized:: class SomeClass(Base): __tablename__ = 'some_table' id = Column(Integer, primary_key=True) name = Column(String(50)) Attributes may be added to the class after its construction, and they will be added to the underlying :class:`~sqlalchemy.schema.Table` and :func:`~sqlalchemy.orm.mapper()` definitions as appropriate:: SomeClass.data = Column('data', Unicode) SomeClass.related = relation(RelatedInfo) Classes which are mapped explicitly using :func:`~sqlalchemy.orm.mapper()` can interact freely with declarative classes. It is recommended, though not required, that all tables share the same underlying :class:`~sqlalchemy.schema.MetaData` object, so that string-configured :class:`~sqlalchemy.schema.ForeignKey` references can be resolved without issue. Association of Metadata and Engine ================================== The :func:`declarative_base` base class contains a :class:`~sqlalchemy.schema.MetaData` object where newly defined :class:`~sqlalchemy.schema.Table` objects are collected. This is accessed via the :class:`~sqlalchemy.schema.MetaData` class level accessor, so to create tables we can say:: engine = create_engine('sqlite://') Base.metadata.create_all(engine) The :class:`~sqlalchemy.engine.base.Engine` created above may also be directly associated with the declarative base class using the ``bind`` keyword argument, where it will be associated with the underlying :class:`~sqlalchemy.schema.MetaData` object and allow SQL operations involving that metadata and its tables to make use of that engine automatically:: Base = declarative_base(bind=create_engine('sqlite://')) Or, as :class:`~sqlalchemy.schema.MetaData` allows, at any time using the ``bind`` attribute:: Base.metadata.bind = create_engine('sqlite://') The :func:`declarative_base` can also receive a pre-created :class:`~sqlalchemy.schema.MetaData` object, which allows a declarative setup to be associated with an already existing traditional collection of :class:`~sqlalchemy.schema.Table` objects:: mymetadata = MetaData() Base = declarative_base(metadata=mymetadata) Configuring Relations ===================== Relations to other classes are done in the usual way, with the added feature that the class specified to :func:`~sqlalchemy.orm.relation()` may be a string name. The "class registry" associated with ``Base`` is used at mapper compilation time to resolve the name into the actual class object, which is expected to have been defined once the mapper configuration is used:: class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String(50)) addresses = relation("Address", backref="user") class Address(Base): __tablename__ = 'addresses' id = Column(Integer, primary_key=True) email = Column(String(50)) user_id = Column(Integer, ForeignKey('users.id')) Column constructs, since they are just that, are immediately usable, as below where we define a primary join condition on the ``Address`` class using them:: class Address(Base): __tablename__ = 'addresses' id = Column(Integer, primary_key=True) email = Column(String(50)) user_id = Column(Integer, ForeignKey('users.id')) user = relation(User, primaryjoin=user_id == User.id) In addition to the main argument for :func:`~sqlalchemy.orm.relation`, other arguments which depend upon the columns present on an as-yet undefined class may also be specified as strings. These strings are evaluated as Python expressions. The full namespace available within this evaluation includes all classes mapped for this declarative base, as well as the contents of the ``sqlalchemy`` package, including expression functions like :func:`~sqlalchemy.sql.expression.desc` and :attr:`~sqlalchemy.sql.expression.func`:: class User(Base): # .... addresses = relation("Address", order_by="desc(Address.email)", primaryjoin="Address.user_id==User.id") As an alternative to string-based attributes, attributes may also be defined after all classes have been created. Just add them to the target class after the fact:: User.addresses = relation(Address, primaryjoin=Address.user_id == User.id) Configuring Many-to-Many Relations ================================== There's nothing special about many-to-many with declarative. The ``secondary`` argument to :func:`~sqlalchemy.orm.relation` still requires a :class:`~sqlalchemy.schema.Table` object, not a declarative class. The :class:`~sqlalchemy.schema.Table` should share the same :class:`~sqlalchemy.schema.MetaData` object used by the declarative base:: keywords = Table('keywords', Base.metadata, Column('author_id', Integer, ForeignKey('authors.id')), Column('keyword_id', Integer, ForeignKey('keywords.id')) ) class Author(Base): __tablename__ = 'authors' id = Column(Integer, primary_key=True) keywords = relation("Keyword", secondary=keywords) You should generally **not** map a class and also specify its table in a many-to-many relation, since the ORM may issue duplicate INSERT and DELETE statements. Defining Synonyms ================= Synonyms are introduced in :ref:`synonyms`. To define a getter/setter which proxies to an underlying attribute, use :func:`~sqlalchemy.orm.synonym` with the ``descriptor`` argument:: class MyClass(Base): __tablename__ = 'sometable' _attr = Column('attr', String) def _get_attr(self): return self._some_attr def _set_attr(self, attr): self._some_attr = attr attr = synonym('_attr', descriptor=property(_get_attr, _set_attr)) The above synonym is then usable as an instance attribute as well as a class-level expression construct:: x = MyClass() x.attr = "some value" session.query(MyClass).filter(MyClass.attr == 'some other value').all() For simple getters, the :func:`synonym_for` decorator can be used in conjunction with ``@property``:: class MyClass(Base): __tablename__ = 'sometable' _attr = Column('attr', String) @synonym_for('_attr') @property def attr(self): return self._some_attr Similarly, :func:`comparable_using` is a front end for the :func:`~sqlalchemy.orm.comparable_property` ORM function:: class MyClass(Base): __tablename__ = 'sometable' name = Column('name', String) @comparable_using(MyUpperCaseComparator) @property def uc_name(self): return self.name.upper() Table Configuration =================== As an alternative to ``__tablename__``, a direct :class:`~sqlalchemy.schema.Table` construct may be used. The :class:`~sqlalchemy.schema.Column` objects, which in this case require their names, will be added to the mapping just like a regular mapping to a table:: class MyClass(Base): __table__ = Table('my_table', Base.metadata, Column('id', Integer, primary_key=True), Column('name', String(50)) ) Other table-based attributes include ``__table_args__``, which is either a dictionary as in:: class MyClass(Base): __tablename__ = 'sometable' __table_args__ = {'mysql_engine':'InnoDB'} or a dictionary-containing tuple in the form ``(arg1, arg2, ..., {kwarg1:value, ...})``, as in:: class MyClass(Base): __tablename__ = 'sometable' __table_args__ = (ForeignKeyConstraint(['id'], ['remote_table.id']), {'autoload':True}) Mapper Configuration ==================== Mapper arguments are specified using the ``__mapper_args__`` class variable, which is a dictionary that accepts the same names as the :class:`~sqlalchemy.orm.mapper` function accepts as keywords:: class Widget(Base): __tablename__ = 'widgets' id = Column(Integer, primary_key=True) __mapper_args__ = {'extension': MyWidgetExtension()} Inheritance Configuration ========================= Declarative supports all three forms of inheritance as intuitively as possible. The ``inherits`` mapper keyword argument is not needed, as declarative will determine this from the class itself. The various "polymorphic" keyword arguments are specified using ``__mapper_args__``. Joined Table Inheritance ~~~~~~~~~~~~~~~~~~~~~~~~ Joined table inheritance is defined as a subclass that defines its own table:: class Person(Base): __tablename__ = 'people' id = Column(Integer, primary_key=True) discriminator = Column('type', String(50)) __mapper_args__ = {'polymorphic_on': discriminator} class Engineer(Person): __tablename__ = 'engineers' __mapper_args__ = {'polymorphic_identity': 'engineer'} id = Column(Integer, ForeignKey('people.id'), primary_key=True) primary_language = Column(String(50)) Note that above, the ``Engineer.id`` attribute, since it shares the same attribute name as the ``Person.id`` attribute, will in fact represent the ``people.id`` and ``engineers.id`` columns together, and will render inside a query as ``"people.id"``. To provide the ``Engineer`` class with an attribute that represents only the ``engineers.id`` column, give it a different attribute name:: class Engineer(Person): __tablename__ = 'engineers' __mapper_args__ = {'polymorphic_identity': 'engineer'} engineer_id = Column('id', Integer, ForeignKey('people.id'), primary_key=True) primary_language = Column(String(50)) Single Table Inheritance ~~~~~~~~~~~~~~~~~~~~~~~~ Single table inheritance is defined as a subclass that does not have its own table; you just leave out the ``__table__`` and ``__tablename__`` attributes:: class Person(Base): __tablename__ = 'people' id = Column(Integer, primary_key=True) discriminator = Column('type', String(50)) __mapper_args__ = {'polymorphic_on': discriminator} class Engineer(Person): __mapper_args__ = {'polymorphic_identity': 'engineer'} primary_language = Column(String(50)) When the above mappers are configured, the ``Person`` class is mapped to the ``people`` table *before* the ``primary_language`` column is defined, and this column will not be included in its own mapping. When ``Engineer`` then defines the ``primary_language`` column, the column is added to the ``people`` table so that it is included in the mapping for ``Engineer`` and is also part of the table's full set of columns. Columns which are not mapped to ``Person`` are also excluded from any other single or joined inheriting classes using the ``exclude_properties`` mapper argument. Below, ``Manager`` will have all the attributes of ``Person`` and ``Manager`` but *not* the ``primary_language`` attribute of ``Engineer``:: class Manager(Person): __mapper_args__ = {'polymorphic_identity': 'manager'} golf_swing = Column(String(50)) The attribute exclusion logic is provided by the ``exclude_properties`` mapper argument, and declarative's default behavior can be disabled by passing an explicit ``exclude_properties`` collection (empty or otherwise) to the ``__mapper_args__``. Concrete Table Inheritance ~~~~~~~~~~~~~~~~~~~~~~~~~~ Concrete is defined as a subclass which has its own table and sets the ``concrete`` keyword argument to ``True``:: class Person(Base): __tablename__ = 'people' id = Column(Integer, primary_key=True) name = Column(String(50)) class Engineer(Person): __tablename__ = 'engineers' __mapper_args__ = {'concrete':True} id = Column(Integer, primary_key=True) primary_language = Column(String(50)) name = Column(String(50)) Usage of an abstract base class is a little less straightforward as it requires usage of :func:`~sqlalchemy.orm.util.polymorphic_union`:: engineers = Table('engineers', Base.metadata, Column('id', Integer, primary_key=True), Column('name', String(50)), Column('primary_language', String(50)) ) managers = Table('managers', Base.metadata, Column('id', Integer, primary_key=True), Column('name', String(50)), Column('golf_swing', String(50)) ) punion = polymorphic_union({ 'engineer':engineers, 'manager':managers }, 'type', 'punion') class Person(Base): __table__ = punion __mapper_args__ = {'polymorphic_on':punion.c.type} class Engineer(Person): __table__ = engineers __mapper_args__ = {'polymorphic_identity':'engineer', 'concrete':True} class Manager(Person): __table__ = managers __mapper_args__ = {'polymorphic_identity':'manager', 'concrete':True} Class Usage =========== As a convenience feature, the :func:`declarative_base` sets a default constructor on classes which takes keyword arguments, and assigns them to the named attributes:: e = Engineer(primary_language='python') Note that ``declarative`` has no integration built in with sessions, and is only intended as an optional syntax for the regular usage of mappers and Table objects. A typical application setup using :func:`~sqlalchemy.orm.scoped_session` might look like:: engine = create_engine('postgres://scott:tiger@localhost/test') Session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base = declarative_base() Mapped instances then make usage of :class:`~sqlalchemy.orm.session.Session` in the usual way. iÿÿÿÿ(tTabletColumntMetaData(tsynonymtmappertcomparable_propertyt class_mapper(tMapperProperty(tPropertyLoadertColumnProperty(t_is_mapped_class(tutilt exceptions(R tdeclarative_baset synonym_fortcomparable_usingtinstrument_declarativecCsSd|ijotid|ƒ‚n||_||_t||i|iƒdS(s¾Given a class, configure the class declaratively, using the given registry (any dictionary) and MetaData object. This operation does not assume any kind of class hierarchy. t_decl_class_registrys4Class %r already has been instrumented declarativelyN(t__dict__R tInvalidRequestErrorRtmetadatat_as_declarativet__name__(tclstregistryR((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyRšs   csh||i|/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyt·st __table__t __tablename__t__table_args__iÿÿÿÿt __autoload__tautoloads8Can't add additional column %r when specifying __table__t__mapper_args__tinheritst__mapper_cls__swClass %r does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.tconcretetcompiletinherit_conditiontignore_nonexistent_tabless?Can't place __table_args__ on an inherited class with no table.sDCan't place primary key columns on an inherited class with no table.texclude_propertiest properties(((3RR t OrderedDictt isinstancettupletlenRRtwarnt_deferred_relationtsortt iteritemsR tcolumnsttabletNonet_undefer_column_nametappendRRRtgettdicttTrueRRRtctcontains_columnR t ArgumentErrortgetattrt __bases__R Rthasattrtunbound_method_to_callableR$RRtFalseRt local_tabletsql_utiltjoin_conditiont primary_keyt append_columntsett_columntopropertytdifference_updatet __mapper__(Rt classnametdict_tktvaluetproptcolsRR;tcolR4t tablenamet table_argstargsttable_kwR!t mapper_argst mapper_clstinherited_mappertinherited_tablet_[1]R)t_[2]((Rs>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR¦s¬   #           +      !     I3tDeclarativeMetacBseZd„Zd„ZRS(cCsMd|ijoti||||ƒSt|||ƒti||||ƒS(NR(Rttypet__init__R(RRLtbasesRM((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR_scCs.d|ijot|tƒo4t||ƒ|ii|ƒ|ii||ƒq*t|tƒolxR|i D]G}t|tƒo1|i djo!t||ƒ|ii|ƒqnqnW|ii||ƒq*t|t ƒo |ii|t ||ƒƒq*ti|||ƒnti|||ƒdS(NRK(RR,RR6RRGRKt add_propertyR R3R4R5RR0R^t __setattr__(RRRORR((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyRb%s      (Rt __module__R_Rb(((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR]s t _GetColumnscBseZd„Zd„ZRS(cCs ||_dS(N(R(tselfR((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR_:scCsrt|idtƒ}|pt|i|ƒS|i|ƒ}t|tƒptid|ƒ‚n|i dSdS(NR&s`Property %r is not an instance of ColumnProperty (i.e. does not correspnd directly to a Column).i( RRRBR>t get_propertyR,R R RR3(ReRRRP((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyt __getattr__<s(RRcR_Rg(((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyRd9s c s䇇fd†}tˆtƒo¾xGd D]?}tˆ|ƒ}t|tƒotˆ|||ƒƒq)q)Wˆiofxcd D]W}|ˆiijo>tˆii|tƒo$|ˆii|ƒˆii|/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyt access_clsKscszy5tˆtƒˆƒ}t|tƒo|iS|SWn>tj o2}tidˆiˆ|i dˆfƒ‚nXdS(Ns¾When compiling mapper %s, expression %r failed to locate a name (%r). If this is a class name, consider adding this relation() to the %r class after both dependent classes have been defined.i( tevaltglobalsR,RdRt NameErrorR RtparentRU(txtn(tdtargRPR(s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyt return_clsRs(RiR t PopulateDict(RrRjRs(RPR(RiRrRqs>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyt resolve_argHs  targumenttorder_byt primaryjoint secondaryjoint secondaryt _foreign_keyst remote_sidet foreign_keys(RvRwRxRyRzR{R|(RxRyRzR}R|Rw(R,RR>t basestringtsetattrtbackreftkwargs(RRPRutattrtv((RPRs>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR0Gs -0cs‡‡fd†}|S(s'Decorator, make a Python @property a query synonym for a column. A decorator version of :func:`~sqlalchemy.orm.synonym`. The function being decorated is the 'descriptor', otherwise passes its arguments through to synonym():: @synonym_for('col') @property def prop(self): return 'special sauce' The regular ``synonym()`` is also usable directly in a declarative setting and may be convenient for read/write properties:: prop = synonym('col', descriptor=property(_read_prop, _write_prop)) cstˆdˆd|ƒS(Nt map_columnt descriptor(t _orm_synonym(tfn(R„tname(s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pytdecorate‚s((RˆR„R‰((R„Rˆs>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyRpscs‡fd†}|S(sDecorator, allow a Python @property to be used in query criteria. A decorator front end to :func:`~sqlalchemy.orm.comparable_property`, passes through the comparator_factory and the function being decorated:: @comparable_using(MyComparatorType) @property def prop(self): return 'special sauce' The regular ``comparable_property()`` is also usable directly in a declarative setting and may be convenient for read/write properties:: prop = comparable_property(MyComparatorType) cs tˆ|ƒS(N(R(R‡(tcomparator_factory(s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR‰—s((RŠR‰((RŠs>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR†scKsbx[|D]S}tt|ƒ|ƒp#td|t|ƒifƒ‚nt||||ƒqWdS(sîA simple constructor that allows initialization from kwargs. Sets kwargs on the constructed instance. Only keys that are present as attributes of type(self) are allowed (for example, any mapped column or relation). s(%r is an invalid keyword argument for %sN(R@R^t TypeErrorRR(ReRRN((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyt_declarative_constructor›sR_tBasec Cs¥|ptƒ}|p|o|p||_nt|tƒ o |fp|} tdtƒd|ƒ} |o|| d/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR «s0! cCs>|idjo ||_n|idjo ||_ndS(N(RR5Rˆ(Rtcolumn((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyR6ês N(sdeclarative_bases synonym_forscomparable_usingsinstrument_declarative('t__doc__tsqlalchemy.schemaRRRtsqlalchemy.ormRR†RRRtsqlalchemy.orm.interfacesRtsqlalchemy.orm.propertiesRR tsqlalchemy.orm.utilR RiR R tsqlalchemy.sqlRDt__all__RRR^R]tobjectRdR0RBRRRŒRR5R R6(((s>/usr/lib/python2.6/site-packages/sqlalchemy/ext/declarative.pyts*" w )     =