models.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Database models"""
  2. from sqlalchemy import Column, ForeignKey, Integer, String, \
  3. DateTime, BigInteger, Date, Boolean
  4. from sqlalchemy.orm import relationship, backref
  5. from sqlalchemy.ext.declarative import declarative_base
  6. Base = declarative_base()
  7. class Player(Base):
  8. """Model for player"""
  9. __tablename__ = 'player'
  10. id = Column(BigInteger, primary_key=True)
  11. name = Column(String)
  12. nation = Column(String)
  13. registration_date = Column(Date)
  14. class TelegramAccount(Base):
  15. """Model for Telegram account"""
  16. __tablename__ = 'telegram_account'
  17. id = Column(BigInteger, primary_key=True)
  18. name = Column(String)
  19. registration_date = Column(DateTime)
  20. class TelegramHandle(Base):
  21. """Model for Telegram handle"""
  22. __tablename__ = 'telegram_handle'
  23. id = Column(Integer, primary_key=True)
  24. handle = Column(String)
  25. registration_date = Column(DateTime)
  26. telegram_account_id = Column(BigInteger, ForeignKey('telegram_account.id'))
  27. telegram_account = relationship(
  28. 'TelegramAccount',
  29. backref=backref('account_handles', lazy='dynamic')
  30. )
  31. class PlayerTelegram(Base):
  32. """Model for belongs to"""
  33. __tablename__ = 'player_telegram'
  34. player_id = Column(BigInteger, ForeignKey('player.id'), primary_key=True)
  35. telegram_id = Column(BigInteger, ForeignKey('telegram_account.id'), primary_key=True)
  36. from_date_time = Column(DateTime, primary_key=True)
  37. until_date_time = Column(DateTime)
  38. player = relationship(
  39. 'Player',
  40. backref=backref('player_telegram', lazy='dynamic')
  41. )