-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmodels.py
More file actions
61 lines (45 loc) · 1.61 KB
/
models.py
File metadata and controls
61 lines (45 loc) · 1.61 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
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Table
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
class Author(Base):
__tablename__ = "authors"
id = Column(Integer, primary_key=True)
nickname = Column(String(50), nullable=False, unique=True)
email = Column(String(50), nullable=False, unique=True)
first_name = Column(String(50))
last_name = Column(String(50))
articles = relationship("Article", back_populates="author")
class Article(Base):
__tablename__ = "articles"
id = Column(Integer, primary_key=True)
title = Column(String(50), nullable=False)
content = Column(Text)
created_on = Column(DateTime, default=datetime.now)
modified_on = Column(
DateTime,
default=datetime.now,
onupdate=datetime.now
)
author_id = Column(Integer, ForeignKey("authors.id"))
author = relationship("Author", back_populates="articles")
hashtags = relationship(
"Hashtag",
secondary="articles_hashtags",
back_populates="articles"
)
class Hashtag(Base):
__tablename__ = "hashtags"
id = Column(Integer, primary_key=True)
hashtag = Column(String(20), nullable=False, unique=True)
articles = relationship(
"Article",
secondary="articles_hashtags",
back_populates="hashtags"
)
association_table = Table(
"articles_hashtags",
Base.metadata,
Column("article_id", ForeignKey("articles.id"), primary_key=True),
Column("hashtag_id", ForeignKey("hashtags.id"), primary_key=True),
)