models.py 6.3 KB
Newer Older
1 2 3
# coding: utf-8

from app import db
4
from sqlalchemy.ext.hybrid import hybrid_property
5 6 7


class Artist(db.Model):
8
    __tablename__ = "artists"
9 10
    artist_id = db.Column("ArtistId", db.Integer, primary_key=True)
    name = db.Column("Name", db.String(120))
D
Doug Farrell 已提交
11
    albums = db.relationship("Album", backref="artist")
12 13 14


class Employee(db.Model):
15
    __tablename__ = "employees"
16 17 18 19
    employee_id = db.Column("EmployeeId", db.Integer, primary_key=True)
    last_name = db.Column("LastName", db.String(20), nullable=False)
    first_name = db.Column("FirstName", db.String(20), nullable=False)
    title = db.Column("Title", db.String(30))
20 21 22 23 24 25
    reports_to_id = db.Column(
        "ReportsTo", db.ForeignKey("employees.EmployeeId"), index=True
    )
    reports_to = db.relationship(
        "Employee", uselist=False, remote_side=[employee_id]
    )
26 27 28 29 30 31 32 33 34 35
    birth_date = db.Column("BirthDate", db.DateTime)
    hire_date = db.Column("HireDate", db.DateTime)
    address = db.Column("Address", db.String(70))
    city = db.Column("City", db.String(40))
    state = db.Column("State", db.String(40))
    country = db.Column("Country", db.String(40))
    postal_code = db.Column("PostalCode", db.String(10))
    phone = db.Column("Phone", db.String(24))
    fax = db.Column("Fax", db.String(24))
    email = db.Column("Email", db.String(60))
36 37 38 39 40 41 42
    parent = db.relationship(
        "Employee", remote_side=[employee_id], backref="reporting"
    )

    @hybrid_property
    def full_name(self):
        return f"{self.first_name} {self.last_name}"
43 44 45


class Genre(db.Model):
46
    __tablename__ = "genres"
47 48
    genre_id = db.Column("GenreId", db.Integer, primary_key=True)
    name = db.Column("Name", db.String(120))
D
Doug Farrell 已提交
49
    tracks = db.relationship("Track", backref="genre")
50 51 52


class MediaType(db.Model):
53
    __tablename__ = "media_types"
54 55
    media_type_id = db.Column("MediaTypeId", db.Integer, primary_key=True)
    name = db.Column("Name", db.String(120))
D
Doug Farrell 已提交
56
    tracks = db.relationship("Track", backref="media_type")
57 58 59


class Playlist(db.Model):
60
    __tablename__ = "playlists"
61 62
    playlist_id = db.Column("PlaylistId", db.Integer, primary_key=True)
    name = db.Column("Name", db.String(120))
63 64 65
    tracks = db.relationship(
        "Track", secondary="playlist_track", back_populates="playlists"
    )
66 67 68


class Album(db.Model):
69
    __tablename__ = "albums"
70 71
    album_id = db.Column("AlbumId", db.Integer, primary_key=True)
    title = db.Column("Title", db.String(160), nullable=False)
72 73 74 75 76 77
    artist_id = db.Column(
        "ArtistId",
        db.ForeignKey("artists.ArtistId"),
        nullable=False,
        index=True,
    )
D
Doug Farrell 已提交
78
    tracks = db.relationship("Track", backref="album")
79

80

81
class Customer(db.Model):
82
    __tablename__ = "customers"
83 84 85 86 87 88 89 90 91 92 93 94
    customer_id = db.Column("CustomerId", db.Integer, primary_key=True)
    first_name = db.Column("FirstName", db.String(40), nullable=False)
    last_name = db.Column("LastName", db.String(20), nullable=False)
    company = db.Column("Company", db.String(80))
    address = db.Column("Address", db.String(70))
    city = db.Column("City", db.String(40))
    state = db.Column("State", db.String(40))
    country = db.Column("Country", db.String(40))
    postal_code = db.Column("PostalCode", db.String(10))
    phone = db.Column("Phone", db.String(24))
    fax = db.Column("Fax", db.String(24))
    email = db.Column("Email", db.String(60), nullable=False)
95 96 97 98 99 100
    support_rep_id = db.Column(
        "SupportRepId", db.ForeignKey("employees.EmployeeId"), index=True
    )
    support_rep = db.relationship(
        "Employee", backref=db.backref("customers", uselist=False)
    )
D
Doug Farrell 已提交
101
    invoices = db.relationship("Invoice", backref="customer")
102

103 104 105 106 107
    @hybrid_property
    def full_name(self):
        return f"{self.first_name} {self.last_name}"


108
class Invoice(db.Model):
109
    __tablename__ = "invoices"
110
    invoice_id = db.Column("InvoiceId", db.Integer, primary_key=True)
111 112 113 114 115 116
    customer_id = db.Column(
        "CustomerId",
        db.ForeignKey("customers.CustomerId"),
        nullable=False,
        index=True,
    )
117 118 119 120 121 122
    invoice_date = db.Column("InvoiceDate", db.DateTime, nullable=False)
    billing_address = db.Column("BillingAddress", db.String(70))
    billing_city = db.Column("BillingCity", db.String(40))
    billing_state = db.Column("BillingState", db.String(40))
    billing_country = db.Column("BillingCountry", db.String(40))
    billing_postal_code = db.Column("BillingPostalCode", db.String(10))
123
    total = db.Column("Total", db.Float, nullable=False)
124
    invoice_items = db.relationship("InvoiceItem", backref="invoice")
125 126 127


class Track(db.Model):
128
    __tablename__ = "tracks"
129 130 131

    track_id = db.Column("TrackId", db.Integer, primary_key=True)
    name = db.Column("Name", db.String(200), nullable=False)
132 133 134 135 136 137 138 139 140 141 142 143
    album_id = db.Column(
        "AlbumId", db.ForeignKey("albums.AlbumId"), index=True
    )
    media_type_id = db.Column(
        "MediaTypeId",
        db.ForeignKey("media_types.MediaTypeId"),
        nullable=False,
        index=True,
    )
    genre_id = db.Column(
        "GenreId", db.ForeignKey("genres.GenreId"), index=True
    )
144 145 146
    composer = db.Column("Composer", db.String(220))
    milliseconds = db.Column("Milliseconds", db.Integer, nullable=False)
    bytes = db.Column("Bytes", db.Integer)
147
    unit_price = db.Column("UnitPrice", db.Float, nullable=False)
D
Doug Farrell 已提交
148
    invoice_items = db.relationship("InvoiceItem", backref="track")
149 150 151 152
    playlists = db.relationship(
        "Playlist", secondary="playlist_track", back_populates="tracks"
    )

153 154

class InvoiceItem(db.Model):
155
    __tablename__ = "invoice_items"
156
    invoice_line_id = db.Column("InvoiceLineId", db.Integer, primary_key=True)
157 158 159 160 161 162 163 164 165 166
    invoice_id = db.Column(
        "InvoiceId",
        db.ForeignKey("invoices.InvoiceId"),
        nullable=False,
        index=True,
    )
    track_id = db.Column(
        "TrackId", db.ForeignKey("tracks.TrackId"), nullable=False, index=True
    )
    unit_price = db.Column("UnitPrice", db.Float, nullable=False)
167 168 169
    quantity = db.Column("Quantity", db.Integer, nullable=False)


D
Doug Farrell 已提交
170
playlist_track = db.Table(
171
    "playlist_track",
D
Doug Farrell 已提交
172
    db.Column(
173 174 175 176 177
        "PlaylistId",
        db.Integer,
        db.ForeignKey("playlists.PlaylistId"),
        primary_key=True,
        nullable=False,
D
Doug Farrell 已提交
178 179
    ),
    db.Column(
180 181 182 183 184 185 186
        "TrackId",
        db.Integer,
        db.ForeignKey("tracks.TrackId"),
        primary_key=True,
        nullable=False,
        index=True,
    ),
187
)