-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
55 lines (40 loc) · 1.42 KB
/
models.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
from atom.api import Atom, ContainerList, Unicode, Int, Enum, Bool, Typed, observe, ForwardInstance, List, Instance
from atomdb.sql import SQLModel, SQLModelManager, Relation
from aiopg.sa import create_engine
import sqlalchemy as sa
import asyncio
import os
from datetime import datetime
class User(SQLModel):
id = Typed(int).tag(type=sa.BigInteger(), primary_key=True)
name = Unicode()
screen_name = Unicode()
followers_count = Int(0)
statuses_count = Int(0)
follower = Bool(False)
tweets = Relation(lambda: Tweet)
class Meta:
db_table = 'user'
class Tweet(SQLModel):
id = Typed(int).tag(type=sa.BigInteger(), primary_key=True)
text = Unicode()
retweet = Bool(False)
created_at = Instance(datetime)
user = Instance(User).tag(nullable=False)
coin = Unicode(default='')
class Meta:
db_table = 'tweet'
async def create_db_tables():
async with create_engine(user='youpsla',
database='deviant',
host='127.0.0.1',
password='372010',
port=5432) as engine:
mgr = SQLModelManager.instance()
mgr.database = engine
mgr.create_tables()
await User.objects.create()
await Tweet.objects.create()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(create_db_tables())