Skip to content

Tortoise ORM fails to generate tables for non-default schemas  #1671

@Pritish053

Description

@Pritish053

Describe the bug
Tortoise ORM has issues generating schemas when a specific schema other than the default public schema is specified. When attempting to use a schema other than public, the ORM does not create the tables automatically.

To Reproduce

from fastapi import FastAPI
from pydantic import BaseModel
from tortoise import Tortoise, fields, models
from tortoise.contrib.fastapi import register_tortoise

app = FastAPI()

class NameCreate(BaseModel):
    name: str

class Name(models.Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255)
    timestamp = fields.DatetimeField(auto_now_add=True)
    
    class Meta:
        table = "names"
        schema = "pgdev"

@app.on_event("startup")
async def init_db():
    await Tortoise.init(
        db_url="postgres://postgres:pgpass@localhost:5455/pgdatabase",
        modules={"models": ["main"]}
    )
    await Tortoise.generate_schemas()

@app.post("/names/")
async def create_name(name: NameCreate):
    name_obj = await Name.create(name=name.name)
    return {"message": f"Hello, {name_obj.name}"}

register_tortoise(
    app,
    db_url="postgres://postgres:pgpass@localhost:5455/pgdatabase",
    modules={"models": ["main"]},
    generate_schemas=True,
    add_exception_handlers=True,
)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Expected behavior
The names table should be created within the pgdev schema, and the record should be inserted successfully.

Actual behaviour
tortoise.exceptions.OperationalError: relation "pgdev.names" does not exist

Environment

•	Tortoise-ORM Version: 0.21.3
•	Tortoise Version: 0.1.1
•	Python Version: 3.10
•	Database: PostgreSQL
•	asyncpg Version: 0.29.0
•	OS: macOS 14.5 (23F79), Apple Silicon

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions