Add project setup some models and a test
This commit is contained in:
39
src/allmende_payment_system/models.py
Normal file
39
src/allmende_payment_system/models.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from sqlalchemy import Column, ForeignKey, Table
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
|
||||
TABLE_PREFIX = "aps_"
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class Account(Base):
|
||||
__tablename__ = TABLE_PREFIX + "account"
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(nullable=False, unique=True)
|
||||
|
||||
users: Mapped[list["User"]] = relationship(
|
||||
"User",
|
||||
secondary=TABLE_PREFIX + "user_account_association",
|
||||
back_populates="accounts",
|
||||
)
|
||||
|
||||
|
||||
user_account_association = Table(
|
||||
TABLE_PREFIX + "user_account_association",
|
||||
Base.metadata,
|
||||
Column("user_id", ForeignKey(TABLE_PREFIX + "user.id"), primary_key=True),
|
||||
Column("account_id", ForeignKey(TABLE_PREFIX + "account.id"), primary_key=True),
|
||||
)
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = TABLE_PREFIX + "user"
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
username: Mapped[str] = mapped_column(nullable=False, unique=True)
|
||||
display_name: Mapped[str] = mapped_column(nullable=False)
|
||||
|
||||
accounts: Mapped[list["Account"]] = relationship(
|
||||
"Account", secondary=user_account_association, back_populates="users"
|
||||
)
|
||||
Reference in New Issue
Block a user