Add the rest of the tables as SQLModels.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy.dialects.mysql import BIGINT, SMALLINT
|
||||
from sqlalchemy.dialects.mysql import BIGINT, INTEGER, SMALLINT
|
||||
from sqlmodel import Column, Field, ForeignKey, Relationship, Session, SQLModel, UniqueConstraint, create_engine
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class Guild(SQLModel, table=True):
|
||||
|
||||
__tablename__: str = "Guilds"
|
||||
|
||||
id: int = Field(sa_column=Column(BIGINT(unsigned=True), primary_key=True))
|
||||
id: int = Field(sa_column=Column(BIGINT(unsigned=True), primary_key=True, nullable=False))
|
||||
name: str = Field(max_length=95)
|
||||
submission_channel: int = Field(sa_column=Column(BIGINT(unsigned=True)))
|
||||
verification_channel: int = Field(sa_column=Column(BIGINT(unsigned=True)))
|
||||
@@ -46,7 +46,13 @@ class CategoryGroup(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
name: str = Field(max_length=50)
|
||||
channel_id: int = Field(sa_column=Column(BIGINT(unsigned=True)))
|
||||
guild_id: int = Field(sa_column=Column(BIGINT(unsigned=True), ForeignKey("Guilds.id", ondelete="CASCADE")))
|
||||
guild_id: int = Field(
|
||||
sa_column=Column(
|
||||
BIGINT(unsigned=True),
|
||||
ForeignKey("Guilds.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
)
|
||||
message_id: int = Field(sa_column=Column(BIGINT(unsigned=True)))
|
||||
pad_length: int = Field(default=12, sa_column=Column(SMALLINT(unsigned=True)))
|
||||
username_length: int = Field(default=12, sa_column=Column(SMALLINT(unsigned=True)))
|
||||
@@ -56,6 +62,64 @@ class CategoryGroup(SQLModel, table=True):
|
||||
guild: Guild | None = Relationship(back_populates="category_groups")
|
||||
|
||||
|
||||
class Category(SQLModel, table=True):
|
||||
__tablename__: str = "Categories"
|
||||
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
name: str = Field(max_length=30)
|
||||
group_id: int = Field(
|
||||
sa_column=Column(
|
||||
INTEGER(unsigned=True),
|
||||
ForeignKey("CategoryGroups.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
)
|
||||
size: int = Field(sa_column=Column(SMALLINT(unsigned=True), default=3, nullable=False))
|
||||
is_ascending: bool = Field(default=True, nullable=False)
|
||||
is_time_record: bool = Field(default=False, nullable=False)
|
||||
is_validated: bool = Field(default=True, nullable=False)
|
||||
|
||||
__table_args__ = (UniqueConstraint("group_id", "name", name="UniqueCategoryInGroup"),)
|
||||
|
||||
|
||||
class Score(SQLModel, table=True):
|
||||
__tablename__: str = "Scores"
|
||||
|
||||
username: str = Field(max_length=30, nullable=False, primary_key=True)
|
||||
score: float = Field(nullable=False)
|
||||
category_id: int = Field(
|
||||
sa_column=Column(
|
||||
INTEGER(unsigned=True),
|
||||
ForeignKey("Categories.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
primary_key=True,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class Submission(SQLModel, table=True):
|
||||
__tablename__: str = "Submissions"
|
||||
|
||||
category_id: int = Field(
|
||||
sa_column=Column(
|
||||
INTEGER(unsigned=True),
|
||||
nullable=False,
|
||||
primary_key=True,
|
||||
),
|
||||
)
|
||||
group_id: int = Field(
|
||||
sa_column=Column(
|
||||
INTEGER(unsigned=True),
|
||||
nullable=False,
|
||||
)
|
||||
)
|
||||
guild_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False))
|
||||
message_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False))
|
||||
score: str = Field(max_length=30, nullable=False)
|
||||
user_id: int = Field(sa_column=Column(BIGINT(unsigned=True), nullable=False))
|
||||
username: str = Field(max_length=30, nullable=False, primary_key=True)
|
||||
|
||||
|
||||
url = "sqlite:///database.db"
|
||||
engine = create_engine(url)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
@@ -65,7 +129,7 @@ with Session(engine) as session:
|
||||
session.add(guild)
|
||||
|
||||
group1 = CategoryGroup(name="group1", channel_id=1, guild_id=1, message_id=1)
|
||||
group2 = CategoryGroup(name="group1", channel_id=2, guild_id=1, message_id=2)
|
||||
group2 = CategoryGroup(name="group2", channel_id=2, guild_id=1, message_id=2)
|
||||
session.add(group1)
|
||||
session.add(group2)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user