From 4ffd1ce8b78ffbe623bcee426a06874189cbbd70 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 20 Dec 2017 12:06:12 +0100 Subject: [PATCH] Add Session and create_all to database.py --- database.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/database.py b/database.py index 22be082..d098679 100644 --- a/database.py +++ b/database.py @@ -1,5 +1,6 @@ from sqlalchemy import create_engine, Column, ForeignKey, UniqueConstraint, CheckConstraint from sqlalchemy import Integer, BigInteger, String, Numeric, Text +from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base import configloader import telegram @@ -13,6 +14,9 @@ engine = create_engine(configloader.config["Database"]["engine"]) # Create a base class to define all the database subclasses TableDeclarativeBase = declarative_base(bind=engine) +# Create a Session class able to initialize database sessions +Session = sessionmaker() + # Define all the database tables using the sqlalchemy declarative base class User(TableDeclarativeBase): """A Telegram user who used the bot at least once.""" @@ -92,7 +96,7 @@ class Transaction(TableDeclarativeBase): # The internal transaction ID transaction_id = Column(BigInteger, primary_key=True) # The user whose credit is affected by this transaction - user_id = Column(BigInteger, ForeignKey("users.id"), nullable=False) + user_id = Column(BigInteger, ForeignKey("users.user_id"), nullable=False) # The value of this transaction. Can be both negative and positive. value = Column(Numeric, nullable=False) # Extra notes on the transaction @@ -109,8 +113,7 @@ class Transaction(TableDeclarativeBase): # Extra table parameters __tablename__ = "transactions" - __table_args__ = (UniqueConstraint("provider", "provider_id"), - CheckConstraint("payment_email ~ '[^@]+@.+'")) # TODO: check this regex + __table_args__ = (UniqueConstraint("provider", "provider_id"),) def __str__(self): """Return the correctly formatted transaction value""" @@ -138,3 +141,8 @@ class Admin(TableDeclarativeBase): def __repr__(self): return f"" + + +# If this script is ran as main, try to create all the tables in the database +if __name__ == "__main__": + TableDeclarativeBase.metadata.create_all() \ No newline at end of file