tortoise / tortoise/tortoise-orm

Generated SQL missing fk in table definition

Open
#2,119 1 comment 0 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

Describe the bug
During migrate, the created SQL does not contain fk field.

To Reproduce
APP A:

class User(Model):
  id=...

APP B:
Create an abstract base class with an FK field

class GenericModel(Model):
  user = fields.ForeignKeyField(...)

Inherit through multiple abstract classes

ModelA(GenericModel):
  ....

ModelB(ModelA):
  ....

Create a concrete model

ConcreteModel(ModelB):
  ...

Run tortoise migrate APP B
Inspect the model metadata

async def create_model(self, model: type[Model]) -> None:
    model_sql_data = self._get_model_sql_data(model)

    model_statement = "\n".join([model_sql_data.table_sql, *model_sql_data.m2m_tables_sql])
    print("\n\nStatement: ")
    print(model_statement)
    await self._run_sql(model_statement)

Expected behavior
Created SQL has all the fields of the table.

Additional context
in tortoise/migrations/schema_editor/base.py
_get_model_sql_data creates sql from fields_db_projection which does not contain fk field.

The migration CLI builds an in-memory “state” of historical models for each app (in tortoise/migrations/schema_generator/state.py). When applying a migration, it calls state.reload_models(models_to_reload) for that app. Critically, only the current app’s models are loaded into the state. Any referenced models from other apps are not automatically included.

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 in tortoise/migrations/schema_editor/base.py at _get_model_sql_data and trace how fields_db_projection is populated for inherited foreign-key fields. Then inspect tortoise/migrations/schema_generator/state.py, especially reload_models(models_to_reload), and reproduce the multi-app abstract-model case from the issue. Done means the generated table SQL includes the foreign-key field when referenced models belong to another app, with regression coverage for the migration path.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.