-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmodels.py
More file actions
76 lines (57 loc) · 1.88 KB
/
models.py
File metadata and controls
76 lines (57 loc) · 1.88 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
from datetime import datetime
from sqlalchemy import (
Column,
String,
Integer,
DateTime,
ForeignKey,
Text,
Table
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
articles_hashtags = Table(
"articles_hashtags",
Base.metadata,
Column("article_id", Integer, ForeignKey("articles.id")),
Column("hashtag_id", Integer, ForeignKey("hashtags.id"))
)
class Author(Base):
__tablename__ = "authors"
id = Column(Integer, primary_key=True)
fullname = Column(String(50))
lastname = Column(String(50))
nickname = Column(String(50), unique=True, nullable=False)
email = Column(String(50), unique=True, nullable=False)
registration_date = Column(DateTime, default=datetime.now)
articles = relationship("Articles", back_populates="author")
def __repr__(self):
return f"Author({self.nickname})"
class Hashtag(Base):
__tablename__ = "hashtags"
id = Column(Integer, primary_key=True)
name = Column(String(50), unique=True, nullable=False)
creation_date = Column(DateTime, default=datetime.now)
articles = relationship(
"Articles",
secondary=articles_hashtags,
back_populates="hashtags"
)
def __repr__(self):
return f"Hashtag({self.name})"
class Articles(Base):
__tablename__ = "articles"
id = Column(Integer, primary_key=True)
title = Column(String(70), nullable=False, unique=True)
content = Column(Text, nullable=False)
creation_date = Column(DateTime, default=datetime.now)
author_id = Column(Integer, ForeignKey("authors.id"))
author = relationship("Author", back_populates="articles")
hashtags = relationship(
"Hashtag",
secondary=articles_hashtags,
back_populates="articles"
)
def __repr__(self):
return f"Article({self.title})"