Skip to content

Linked list recursive query #11490

Discussion options

You must be logged in to vote

Perhaps this simplified example might give you an idea on how to proceed:

from pprint import pprint
from typing import Optional

from sqlalchemy import create_engine, ForeignKey, insert, select, literal_column
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session


class Base(DeclarativeBase):
    pass


class Person(Base):
    __tablename__ = "person"
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=False)
    name: Mapped[str]
    parent_id: Mapped[Optional[int]] = mapped_column(ForeignKey("person.id"))


engine = create_engine("postgresql://scott:tiger@192.168.0.199/test")
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

person_table

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@khoroshevskyi
Comment options

Answer selected by khoroshevskyi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants