-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
50 lines (38 loc) · 1.57 KB
/
models.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python2
# coding: utf-8
from sqlalchemy import Column, ForeignKey, UniqueConstraint, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
Base = declarative_base()
class Users(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
email = Column(String(250), nullable=False)
picture = Column(String(250))
class Categories(Base):
__tablename__ = "categories"
id = Column(Integer, primary_key=True)
name = Column(String(100, collation="NOCASE"), nullable=False)
class Items(Base):
"""
O app não permite guardar items com o mesmo nome na mesma categoria visto
que os endpoints utilizam os nomes dos mesmos. Por isso foi necessário a
inclusão de uma Unique Constraint
"""
__tablename__ = "items"
__table_args__ = (
UniqueConstraint("category_id", "title", name="un_title"),
)
id = Column(Integer, primary_key=True)
# Atributo "collation" necessário para que o case dos nomes seja ignorado
title = Column(String(100, collation="NOCASE"), nullable=False)
description = Column(String(2000), nullable=False)
category_id = Column(Integer, ForeignKey("categories.id"))
category = relationship(Categories)
user_id = Column(Integer, ForeignKey("users.id"))
user = relationship(Users)
engine = create_engine("sqlite:///catalog.db",
connect_args={"check_same_thread": False})
Base.metadata.create_all(engine)