Hello, I am Andy.
I work as fulltime junior web developer, but most of my time I spend in Typescript and by doing frontend. I was tasked to get closer with Python and FastAPI. Its really nice framework and it has really good documentation, but I keep strugling with SQLAlchemy and the relations.
I am basically working on a demo app a tiny social network. Like old facebook. You can create a post, like a post and request/accept/reject a friendship. Now that is the catch. I dont know how to make the relation between friends. (just like IRL :D)
I am using SQLmodel library by the same author as the FastAPI framework. It uses sqlalchemy with pydantic under the hood. Here is the schema I came up with:
“`python from sqlmodel import SQLModel, Field, Relationship from datetime import datetime, timezone from typing import List, Optional from enum import Enum
——————- UTILS ——————-
class FriendshipStatus(str, Enum): pending = “pending” accepted = “accepted” rejected = “rejected”
def now() -> datetime: return datetime.now(timezone.utc)
——————- DATABASE MODELS ——————-
class DbUser(SQLModel, table=True): id: Optional[int] = Field(primary_key=True, default=None) username: str = Field(index=True) email: str = Field(index=True) hashed_password: str created_at: datetime = Field(default_factory=now) updated_at: datetime = Field(default_factory=now)
active_relationships: List["DbFriendship"] = Relationship(back_populates="user", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.user_id]"}) pending_relationships: List["DbFriendship"] = Relationship(back_populates="friend", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.friend_id]"})
class DbFriendship(SQLModel, table=True): id: Optional[int] = Field(primary_key=True, default=None) status: FriendshipStatus = Field(default=FriendshipStatus.pending) created_at: datetime = Field(default_factory=now) updated_at: datetime = Field(default_factory=now)
user_id: int = Field(foreign_key="dbuser.id") friend_id: int = Field(foreign_key="dbuser.id") user: DbUser = Relationship(back_populates="active_relationships", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.user_id]"}) friend: DbUser = Relationship(back_populates="pending_relationships", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.friend_id]"})
“`
Basically the idea is to have a separate table, where we hold information about the relation status of two users. This works well until it is time for getting the relations themselfs. When I try to send a user with all relations I get a recursion error. I understand why, but have no idea how to fix it.
I should add, that I have no big experience where I would work with databases. Thank you for all your help, I really value it.
submitted by /u/IAsqitI
[link] [comments]
r/learnpython Hello, I am Andy. I work as fulltime junior web developer, but most of my time I spend in Typescript and by doing frontend. I was tasked to get closer with Python and FastAPI. Its really nice framework and it has really good documentation, but I keep strugling with SQLAlchemy and the relations. I am basically working on a demo app a tiny social network. Like old facebook. You can create a post, like a post and request/accept/reject a friendship. Now that is the catch. I dont know how to make the relation between friends. (just like IRL :D) I am using SQLmodel library by the same author as the FastAPI framework. It uses sqlalchemy with pydantic under the hood. Here is the schema I came up with: “`python from sqlmodel import SQLModel, Field, Relationship from datetime import datetime, timezone from typing import List, Optional from enum import Enum ——————- UTILS ——————- class FriendshipStatus(str, Enum): pending = “pending” accepted = “accepted” rejected = “rejected” def now() -> datetime: return datetime.now(timezone.utc) ——————- DATABASE MODELS ——————- class DbUser(SQLModel, table=True): id: Optional[int] = Field(primary_key=True, default=None) username: str = Field(index=True) email: str = Field(index=True) hashed_password: str created_at: datetime = Field(default_factory=now) updated_at: datetime = Field(default_factory=now) active_relationships: List[“DbFriendship”] = Relationship(back_populates=”user”, sa_relationship_kwargs={“foreign_keys”: “[DbFriendship.user_id]”}) pending_relationships: List[“DbFriendship”] = Relationship(back_populates=”friend”, sa_relationship_kwargs={“foreign_keys”: “[DbFriendship.friend_id]”}) class DbFriendship(SQLModel, table=True): id: Optional[int] = Field(primary_key=True, default=None) status: FriendshipStatus = Field(default=FriendshipStatus.pending) created_at: datetime = Field(default_factory=now) updated_at: datetime = Field(default_factory=now) user_id: int = Field(foreign_key=”dbuser.id”) friend_id: int = Field(foreign_key=”dbuser.id”) user: DbUser = Relationship(back_populates=”active_relationships”, sa_relationship_kwargs={“foreign_keys”: “[DbFriendship.user_id]”}) friend: DbUser = Relationship(back_populates=”pending_relationships”, sa_relationship_kwargs={“foreign_keys”: “[DbFriendship.friend_id]”}) “` Basically the idea is to have a separate table, where we hold information about the relation status of two users. This works well until it is time for getting the relations themselfs. When I try to send a user with all relations I get a recursion error. I understand why, but have no idea how to fix it. I should add, that I have no big experience where I would work with databases. Thank you for all your help, I really value it. submitted by /u/IAsqitI [link] [comments]
Hello, I am Andy.
I work as fulltime junior web developer, but most of my time I spend in Typescript and by doing frontend. I was tasked to get closer with Python and FastAPI. Its really nice framework and it has really good documentation, but I keep strugling with SQLAlchemy and the relations.
I am basically working on a demo app a tiny social network. Like old facebook. You can create a post, like a post and request/accept/reject a friendship. Now that is the catch. I dont know how to make the relation between friends. (just like IRL :D)
I am using SQLmodel library by the same author as the FastAPI framework. It uses sqlalchemy with pydantic under the hood. Here is the schema I came up with:
“`python from sqlmodel import SQLModel, Field, Relationship from datetime import datetime, timezone from typing import List, Optional from enum import Enum
——————- UTILS ——————-
class FriendshipStatus(str, Enum): pending = “pending” accepted = “accepted” rejected = “rejected”
def now() -> datetime: return datetime.now(timezone.utc)
——————- DATABASE MODELS ——————-
class DbUser(SQLModel, table=True): id: Optional[int] = Field(primary_key=True, default=None) username: str = Field(index=True) email: str = Field(index=True) hashed_password: str created_at: datetime = Field(default_factory=now) updated_at: datetime = Field(default_factory=now)
active_relationships: List["DbFriendship"] = Relationship(back_populates="user", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.user_id]"}) pending_relationships: List["DbFriendship"] = Relationship(back_populates="friend", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.friend_id]"})
class DbFriendship(SQLModel, table=True): id: Optional[int] = Field(primary_key=True, default=None) status: FriendshipStatus = Field(default=FriendshipStatus.pending) created_at: datetime = Field(default_factory=now) updated_at: datetime = Field(default_factory=now)
user_id: int = Field(foreign_key="dbuser.id") friend_id: int = Field(foreign_key="dbuser.id") user: DbUser = Relationship(back_populates="active_relationships", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.user_id]"}) friend: DbUser = Relationship(back_populates="pending_relationships", sa_relationship_kwargs={"foreign_keys": "[DbFriendship.friend_id]"})
“`
Basically the idea is to have a separate table, where we hold information about the relation status of two users. This works well until it is time for getting the relations themselfs. When I try to send a user with all relations I get a recursion error. I understand why, but have no idea how to fix it.
I should add, that I have no big experience where I would work with databases. Thank you for all your help, I really value it.
submitted by /u/IAsqitI
[link] [comments]