telegram-moderator/model.py

43 lines
1.2 KiB
Python
Raw Normal View History

2018-01-28 18:29:58 -05:00
from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base
import os
postgres_url = os.environ["TELEGRAM_BOT_POSTGRES_URL"]
2018-01-28 18:29:58 -05:00
'''
This model has been referenced from: https://www.pythoncentral.io/sqlalchemy-orm-examples/
'''
2018-01-28 18:29:58 -05:00
Base = declarative_base()
2018-01-28 18:29:58 -05:00
class User(Base):
__tablename__ = 'telegram_users'
2018-01-28 18:29:58 -05:00
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
username = Column(String)
2018-01-28 18:29:58 -05:00
class Message(Base):
__tablename__ = 'telegram_messages'
2018-01-28 18:29:58 -05:00
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('telegram_users.id'), nullable=False)
2018-01-28 18:29:58 -05:00
message = Column(String)
# Use default=func.now() to set the default hiring time
# of an Employee to be the current time when an
# Employee record was created
time = Column(DateTime, default=func.now())
2018-01-28 18:29:58 -05:00
from sqlalchemy import create_engine
engine = create_engine(postgres_url)
2018-01-28 18:29:58 -05:00
from sqlalchemy.orm import sessionmaker
session = sessionmaker()
session.configure(bind=engine)
Base.metadata.create_all(engine)
print "Created database model"