File: models.py

package info (click to toggle)
sphinx-sqlalchemy 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 160 kB
  • sloc: python: 241; makefile: 7
file content (30 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Example SQLAlchemy ORM models."""

from sqlalchemy import CheckConstraint, Column, ForeignKey, UniqueConstraint, orm, types

Base = orm.declarative_base()


class User(Base):
    """A ``user``."""

    __tablename__ = "dbusers"
    __table_args__ = (UniqueConstraint("first_name", "last_name"),)
    pk = Column(types.Integer, primary_key=True)
    first_name = Column(types.String, doc="The name of the user.")
    last_name = Column(types.String(255), doc="The surname of the user.")
    dob = Column(types.Date, nullable=False, doc="The date of birth.")


class Address(Base):
    """An address."""

    __tablename__ = "addresses"
    __table_args__ = (CheckConstraint("number>0", name="check1"),)
    pk = Column(types.Integer, primary_key=True)
    number = Column(types.Integer, nullable=False, doc="The number of the address.")
    postcode = Column(
        types.String, nullable=False, index=True, doc="The postcode of the address."
    )
    user_id = Column(types.Integer, ForeignKey("dbusers.pk"))
    user = orm.relationship("User")