-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_test.py
98 lines (75 loc) · 2.86 KB
/
models_test.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
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
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# coding: utf-8
from sqlalchemy import create_engine, Table, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Text, DateTime
from sqlalchemy import func
from sqlalchemy.orm import relationship, backref
from datetime import datetime
engine = create_engine('sqlite:///test.db', echo=True)
Base = declarative_base()
metadata = Base.metadata
entry_tag = Table('entry_tag', Base.metadata,
Column('entry_id', Integer, ForeignKey('entries.id')),
Column('tag_id', Integer, ForeignKey('tags.id')),
)
class User(Base):
__tablename__ = 'user'
# the length is referenced when calling CREATE TABLE
id = Column(Integer(11), primary_key=True)
username = Column(String(100))
passwd = Column(String(50))
def __init__(self, username, passwd):
self.username = username
self.passwd = passwd
class Entry(Base):
__tablename__ = 'entries'
id = Column(Integer(11), primary_key = True)
title = Column(String)
slug = Column(String)
content = Column(Text)
viewNum = Column(Integer, default=1)
commentNum = Column(Integer, default=0)
createdTime = Column(DateTime, default = datetime.now())
modifiedTime = Column(DateTime)
categoryId = Column(Integer, ForeignKey('categories.id'))
category = relationship("Category", backref=backref('entries', order_by=id))
# relation with tags, many-to-many,
tags = relationship("Tag", secondary=entry_tag, backref='entries')
class Category(Base):
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
name = Column(String)
slug = Column(String)
entryNum = Column(Integer)
createdTime = Column(DateTime, default = datetime.now())
modifiedTime = Column(DateTime)
#entries = relationship("Entry", backref='categories')
class Comment(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True)
email = Column(String)
username = Column(String)
url = Column(String)
comment = Column(Text)
createdTime = Column(DateTime, default=datetime.now())
entryId = Column(Integer, ForeignKey('entries.id'))
entry = relationship("Entry", backref=backref('comments', order_by=id))
class Tag(Base):
__tablename__ = 'tags'
id = Column(Integer, primary_key=True)
name = Column(String)
entryNum = Column(Integer, default=0)
#entries = relationship('Entry', secondary=entry_tag, backref='tags')
class Link(Base):
__tablename__ = 'links'
id = Column(Integer, primary_key=True)
name = Column(String)
url = Column(String)
description = Column(String)
createdTime = Column(DateTime, default=datetime.now())
class Admin(Base):
__tablename__ = 'admins'
id = Column(Integer, primary_key=True)
username = Column(String)
passwd = Column(String)