-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodels.py
More file actions
86 lines (65 loc) · 2.2 KB
/
models.py
File metadata and controls
86 lines (65 loc) · 2.2 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import datetime
from sqlalchemy import Column, Integer, String, DateTime, Float, Text, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from session import engine
Base = declarative_base(bind=engine)
class Author(Base):
__tablename__ = "authors"
id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String(50))
last_name = Column(String(50))
login = Column(String(50), unique=True, nullable=False)
salary = Column(Float, default=0)
email = Column(String(50), unique=True, nullable=False)
registration_date = Column(DateTime, default=datetime.datetime.now)
articles = relationship(
"Article",
back_populates="author",
cascade="all, delete, delete-orphan"
)
def __repr__(self):
return f"Author({self.login})"
class Article(Base):
__tablename__ = "articles"
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(100), nullable=False, unique=True)
content = Column(Text, nullable=False)
publication_date = Column(DateTime, default=datetime.datetime.now)
author_id = Column(Integer, ForeignKey("authors.id"), nullable=False)
author = relationship("Author", back_populates="articles")
hashtags = relationship(
"Hashtag",
back_populates="articles",
secondary="articles_hashtags"
)
def __repr__(self):
return f"Article({self.title})"
class Hashtag(Base):
__tablename__ = "hashtags"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(50), nullable=False, unique=True)
creation_date = Column(DateTime, default=datetime.datetime.now)
articles = relationship(
"Article",
back_populates="hashtags",
secondary="articles_hashtags"
)
def __repr__(self):
return f"Hashtag({self.name})"
article_hashtag = Table(
"articles_hashtags",
Base.metadata,
Column(
"article_id",
Integer,
ForeignKey("articles.id"),
primary_key=True
),
Column(
"hashtag_id",
Integer,
ForeignKey("hashtags.id"),
primary_key=True
)
)