tortoise / tortoise/tortoise-orm

[Feature] Whether a postgres table can be created on a non-default schema?

Open
#1,409 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Problem related on this feature

  1. I have a large number of tables, and I want to spread them out into different schemas to avoid unnecessary prefixes.
  2. So I define such a class and specify its schema name. This is true of many other classes.
class UserAccount(Model):
    id = IntField(pk=True)

    class Meta:
        schema = "user"
        table = "account"
  1. But Tortoise.generate_schemas don't seem to work as I expected, and all the tables are crowded under the default schema.

Solution
I don't have a solution, and I'm not even sure if I'm missing a solution in the documentation.

Remark
The result of my research so far is that the get_create_schema_sql of AsyncpgSchemaGenerator inherits from BaseSchemaGenerator.
According to the get_create_schema_sql source code, SQL string appears to not reserve placeholders for the schema.

# backends/base/schema_generator.py line:335
table_create_string = self.TABLE_CREATE_TEMPLATE.format(
    exists="IF NOT EXISTS " if safe else "",
    table_name=model._meta.db_table,
    fields=table_fields_string,
    comment=table_comment,
    extra=self._table_generate_extra(table=model._meta.db_table),
)

PackageInfo
Name: tortoise-orm
Version: 0.19.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading backends/base/schema_generator.py, especially get_create_schema_sql, and compare it with AsyncpgSchemaGenerator. Check how Meta.schema and Meta.table are represented in generated SQL; done means generate_schemas creates the requested PostgreSQL schema and table instead of placing the table in the default schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.