Skip to content

How to implement Multi tenancy in FastAPI, SQLAlchemy and Postgresql? #5832

@abdulla-mindfire

Description

@abdulla-mindfire

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlalchemy import Boolean, Column, ForeignKey, Integer, String


class Tenant(Base):
    __tablename__ = "tenants"

    id = Column("id", Integer, primary_key=True, nullable=False)
    name = Column("name", String(256), nullable=False, index=True, unique=True)
    schema = Column("schema", String(256), nullable=False, unique=True)
    host = Column("host", String(256), nullable=False, unique=True)
    

    __table_args__ = ({"schema": "shared"},)

Description

Found an Article but unable to understand how to implement dynamic schema generation when new client sign up on the portal.

so that any client access like

client1.mysite.com
client2.mysite.com

And schema generation should be on the fly if a new client signup

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.88.0

Python Version

3.10.9

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions