Guest User

Untitled

a guest
Nov 6th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. class User(UserMixin, db.Model):  
  2.     __tablename__ = 'user'
  3.  
  4.     '''
  5.    One to Many relationship between 2 tables.
  6.    The One relationship is User and the Many relationships is Payment
  7.    '''
  8.     id: Mapped[int] = mapped_column(primary_key=True)e    
  9.     username: Mapped[str] = mapped_column(String(80), index=True, unique=True)
  10.     email_token: Mapped[Optional[str]] = mapped_column(String())    
  11.  
  12.     rel_payments: Mapped[List["Payments"]] = relationship(back_populates="rel_user")
  13.  
  14.  
  15.    
  16.     def __repr__(self):
  17.         return '<User {}>'.format(self.email)
  18.  
  19.  
  20.  
  21.  
  22.  
  23. class Payments(UserMixin, db.Model):
  24.     '''
  25.    One to Many relationship between 1 table.
  26.    The One relationship is User and the Many relationship is Payments.
  27.    '''
  28.     id: Mapped[int] = mapped_column(primary_key=True)
  29.     item_name: Mapped[str] = mapped_column(String(80))
  30.     price_of_donation: Mapped[int] = mapped_column(Integer)
  31.     # How do I turn email into the foreign key? todo.
  32.     email: Mapped[str] = mapped_column(String(120))
  33.     # The "Many" relationship/connection between the User table + FK
  34.     fk_user_id: Mapped[Optional[int]]= mapped_column(ForeignKey("user.id"))
  35.     rel_user: Mapped[Optional["User"]] = relationship(back_populates="rel_payments")
  36.  
  37.    
  38.     def __repr__(self):
  39.         return '<Payments {}>'.format(self.email)
  40.    
  41.  
Advertisement
Add Comment
Please, Sign In to add comment