Skip to content

Commit

Permalink
add orm definition
Browse files Browse the repository at this point in the history
  • Loading branch information
zcemycl committed Oct 24, 2023
1 parent 7db544d commit 6128273
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/example_package/dataclasses/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from enum import Enum as EnumType

from sqlalchemy import (
Column,
Enum,
Expand All @@ -8,16 +6,13 @@
MetaData,
String,
Table,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB

metadata = MetaData()

from .common import Role

class Role(str, EnumType):
developer = "developer"
maintainer = "maintainer"
viewer = "viewer"
metadata = MetaData()


friendship = Table(
Expand All @@ -39,6 +34,31 @@ class Role(str, EnumType):
index=True,
unique=False,
),
UniqueConstraint(
"parent_person_id", "child_person_id", name="friendship_case"
),
)

person_skill_link = Table(
"person_skill_link",
metadata,
Column(
"person_id",
Integer,
ForeignKey("person.id"),
primary_key=True,
index=True,
unique=False,
),
Column(
"skill_id",
Integer,
ForeignKey("skill.id"),
primary_key=True,
index=True,
unique=False,
),
UniqueConstraint("person_id", "skill_id", name="person_skill_case"),
)

person = Table(
Expand All @@ -50,3 +70,10 @@ class Role(str, EnumType):
Column("others", JSONB),
Column("role", Enum(Role)),
)

skill = Table(
"skill",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String),
)
7 changes: 7 additions & 0 deletions src/example_package/dataclasses/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from enum import Enum as EnumType


class Role(str, EnumType):
developer = "developer"
maintainer = "maintainer"
viewer = "viewer"
102 changes: 102 additions & 0 deletions src/example_package/dataclasses/orm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from sqlalchemy import (
Column,
Enum,
ForeignKey,
Integer,
String,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import declarative_base, relationship

from .common import Role

Base = declarative_base()
metadata = Base.metadata


class friendship(Base):
__tablename__ = "friendship"
parent_person_id = Column(
"parent_person_id",
Integer,
ForeignKey("person.id"),
primary_key=True,
index=True,
unique=False,
)
child_person_id = Column(
"child_person_id",
Integer,
ForeignKey("person.id"),
primary_key=True,
index=True,
unique=False,
)
__table_args__ = (
UniqueConstraint(
"parent_person_id", "child_person_id", name="friendship_case"
),
)


class person_skill_link(Base):
__tablename__ = "person_skill_link"
person_id = Column(
"person_id",
Integer,
ForeignKey("person.id"),
primary_key=True,
index=True,
unique=False,
)
skill_id = Column(
"skill_id",
Integer,
ForeignKey("skill.id"),
primary_key=True,
index=True,
unique=False,
)
__table_args__ = (
UniqueConstraint("person_id", "skill_id", name="person_skill_case"),
)


class person(Base):
__tablename__ = "person"
id = Column("id", Integer, primary_key=True)
firstname = Column("firstname", String)
lastname = Column("lastname", String)
others = Column("others", JSONB)
role = Column("role", Enum(Role))
skills = relationship(
"skill",
secondary=person_skill_link.__table__,
back_populates="persons",
)
parent_friendships = relationship(
"person",
secondary=friendship.__table__,
primaryjoin=id == friendship.parent_person_id,
secondaryjoin=id == friendship.child_person_id,
back_populates="parent_friendships",
)
child_friendships = relationship(
"person",
secondary=friendship.__table__,
primaryjoin=id == friendship.child_person_id,
secondaryjoin=id == friendship.parent_person_id,
back_populates="parent_friendships",
)


class skill(Base):
__tablename__ = "skill"
id = Column("id", Integer, primary_key=True)
name = Column("name", String)
persons = relationship(
"person",
secondary=person_skill_link.__table__,
back_populates="skills",
)

0 comments on commit 6128273

Please sign in to comment.