-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmodels.py
More file actions
77 lines (53 loc) · 1.9 KB
/
models.py
File metadata and controls
77 lines (53 loc) · 1.9 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
import datetime
from sqlalchemy import Column, Integer, String, DateTime, 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)
first_name = Column(String(50))
last_name = Column(String(50))
user_name = Column(String(50), unique=True, nullable=False)
email = Column(String(50), unique=True, nullable=False)
registration_date = Column(
DateTime,
nullable=False,
default=datetime.datetime.now
)
articles = relationship("Article", cascade="all, delete, delete-orphan")
def __repr__(self):
return f"Author({self.user_name})"
class Article(Base):
__tablename__ = "articles"
id = Column(Integer, primary_key=True)
title = Column(String(200), nullable=False, unique=True)
content = Column(Text, nullable=False)
publication_date = Column(
DateTime,
nullable=False,
default=datetime.datetime.now
)
author_id = Column(
Integer,
ForeignKey("authors.id"),
nullable=False,
)
author = relationship("Author")
hashtags = relationship("Hashtag", secondary="articles_hashtags")
def __repr__(self):
return f"Article({self.title})"
class Hashtag(Base):
__tablename__ = "hashtags"
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False, unique=True)
articles = relationship("Article", secondary="articles_hashtags")
def __repr__(self):
return f"Hashtag({self.name})"
articles_hashtags = 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),
)