Add CategoryGroup definition.
Not working completely, the unique constraint is missing.
This commit is contained in:
@@ -1,14 +1,72 @@
|
||||
from sqlmodel import BigInteger, Column, Field, SQLModel, create_engine
|
||||
from sqlalchemy.dialects.mysql import BIGINT, SMALLINT
|
||||
from sqlmodel import Column, Field, ForeignKey, Relationship, Session, SQLModel, UniqueConstraint, create_engine
|
||||
|
||||
|
||||
class Guilds(SQLModel, table=True):
|
||||
id: int = Field(sa_column=Column(BigInteger(), primary_key=True))
|
||||
class Guild(SQLModel, table=True):
|
||||
"""
|
||||
Represents a Guild entity in the database.
|
||||
|
||||
Attributes:
|
||||
id (int): The unique identifier for the guild.
|
||||
name (str): The name of the guild.
|
||||
submission_channel (int): The ID of the submission channel associated with the guild.
|
||||
verification_channel (int): The ID of the verification channel associated with the guild.
|
||||
active (bool): Indicates whether the guild is active. Defaults to True.
|
||||
category_groups (list[CategoryGroup]): A list of category groups associated with the guild.
|
||||
"""
|
||||
|
||||
__tablename__: str = "Guilds"
|
||||
|
||||
id: int = Field(sa_column=Column(BIGINT(unsigned=True), primary_key=True))
|
||||
name: str = Field(max_length=95)
|
||||
submission_channel: int = Field(sa_column=Column(BigInteger()))
|
||||
verification_channel: int = Field(sa_column=Column(BigInteger()))
|
||||
submission_channel: int = Field(sa_column=Column(BIGINT(unsigned=True)))
|
||||
verification_channel: int = Field(sa_column=Column(BIGINT(unsigned=True)))
|
||||
active: bool = Field(default=True)
|
||||
|
||||
category_groups: list["CategoryGroup"] = Relationship(back_populates="guild", cascade_delete=True)
|
||||
|
||||
|
||||
class CategoryGroup(SQLModel, table=True):
|
||||
"""
|
||||
Represents a category group in the database.
|
||||
|
||||
Attributes:
|
||||
id (int | None): The primary key of the category group.
|
||||
name (str): The name of the category group, with a maximum length of 50 characters.
|
||||
channel_id (int): The ID of the channel associated with the category group.
|
||||
guild_id (int): The ID of the guild associated with the category group.
|
||||
message_id (int): The ID of the message associated with the category group.
|
||||
pad_length (int): The padding length for the category group, default is 12.
|
||||
username_length (int): The username length for the category group, default is 12.
|
||||
guild (Guild | None): The guild relationship back-populated from the Guild model.
|
||||
"""
|
||||
|
||||
__tablename__: str = "CategoryGroups"
|
||||
|
||||
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")))
|
||||
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)))
|
||||
|
||||
__table_args__ = (UniqueConstraint("guild_id", "name", name="UniqueGuildCategoryGroup"),)
|
||||
|
||||
guild: Guild | None = Relationship(back_populates="category_groups")
|
||||
|
||||
|
||||
url = "sqlite:///database.db"
|
||||
engine = create_engine(url)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
with Session(engine) as session:
|
||||
guild = Guild(id=1, name="test guild", submission_channel=1, verification_channel=2)
|
||||
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)
|
||||
session.add(group1)
|
||||
session.add(group2)
|
||||
|
||||
session.commit()
|
||||
|
||||
Reference in New Issue
Block a user