Module: fields

This module provides support for defining the fields (columns) of your entities. Elixir currently supports two syntaxes to do so: the default Attribute-based syntax as well as the has_field DSL statement.

Attribute-based syntax

Here is a quick example of how to use the object-oriented syntax.

class Person(Entity):
    id = Field(Integer, primary_key=True)
    name = Field(String(50), required=True)
    ssn = Field(String(50), unique=True)
    biography = Field(Text)
    join_date = Field(DateTime, default=datetime.datetime.now)
    photo = Field(Binary, deferred=True)
    _email = Field(String(20), colname='email', synonym='email')

    def _set_email(self, email):
       self._email = email
    def _get_email(self):
       return self._email
    email = property(_get_email, _set_email)

The Field class takes one mandatory argument, which is its type. Please refer to SQLAlchemy documentation for a list of types supported by SQLAlchemy.

Following that first mandatory argument, fields can take any number of optional keyword arguments. Please note that all the arguments that are not specifically processed by Elixir, as mentioned in the documentation below are passed on to the SQLAlchemy ``Column`` object. Please refer to the SQLAlchemy Column object's documentation for more details about other supported keyword arguments.

The following Elixir-specific arguments are supported:

Argument Name Description
required Specify whether or not this field can be set to None (left without a value). Defaults to False, unless the field is a primary key.
colname Specify a custom name for the column of this field. By default the column will have the same name as the attribute.
deferred Specify whether this particular column should be fetched by default (along with the other columns) when an instance of the entity is fetched from the database or rather only later on when this particular column is first referenced. This can be useful when one wants to avoid loading a large text or binary field into memory when its not needed. Individual columns can be lazy loaded by themselves (by using deferred=True) or placed into groups that lazy-load together (by using deferred = "group_name").
synonym Specify a synonym name for this field. The field will also be usable under that name in keyword-based Query functions such as filter_by. The Synonym class (see the properties module) provides a similar functionality with an (arguably) nicer syntax, but a limited scope.

has_field

The has_field statement allows you to define fields one at a time.

The first argument is the name of the field, the second is its type. Following these, any number of keyword arguments can be specified for additional behavior. The following arguments are supported:

Argument Name Description
through Specify a relation name to go through. This field will not exist as a column on the database but will be a property which automatically proxy values to the attribute attribute of the object pointed to by the relation. If the attribute argument is not present, the name of the current field will be used. In an has_field statement, you can only proxy through a belongs_to or an has_one relationship.
attribute Name of the "endpoint" attribute to proxy to. This should only be used in combination with the through argument.

Here is a quick example of how to use has_field.

class Person(Entity):
    has_field('id', Integer, primary_key=True)
    has_field('name', String(50))

Classes

Field

Represents the definition of a 'field' on an entity.