tortoise / tortoise/tortoise-orm

Foreign Key Columns does not exist

Open
#1,118 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
A clear and concise description of what the bug is.
So I've been working with a pretty complicated system. We use a custom sanic wrapper and tortoise for our models, the catch is we don't run migrations, EVER!. We manually write SQL for our tables and just write a similar models based on it in python.

Recently I encountered this bug and don't have any idea how should I go about solving it. So I have a user_roles models which looks similar to this where the app column FK references a master app table which contains a name column as the source_field. Similarly it is done on the role column as well.

The problem is these source_field="name" does not exist as per the ORM when I'm trying to query. The field does exist as when I query the master App or roles table, I get the data.

The user_roles table also fetched the username from the user table correctly but does not in this case.

Why is this behaviour happening ? Is it because I've also added a Foreign Key constraint on the DB level ?

I've tried changing versions and cleaning up database and loading up new data but nothing seems to work here.

Also as the user_roles Table on the DB contains data, when I switch to a TextField() for these two everything starts working great.

    username = fields.ForeignKeyField(
        "{}.{}".format(CONFIG.config.get("NAME"), "User"),
        related_name="roles",
        on_delete=fields.CASCADE,
        source_field="username",
        pk=True,
    )
    app = fields.ForeignKeyField("{}}".format(CONFIG.config.get("NAME"), "Application"), related_name="app", source_field="name")
    role = fields.ForeignKeyField("{}.{}".format(CONFIG.config.get("NAME"), "Role"), related_name="role", source_field="name")
    # app = CustomTextField(index=True)
    # role = CustomTextField(index=True)
    enabled = fields.BooleanField(default=True, null=False)
    permission = fields.TextField(null=True)

This is the actual error I got.

695de42a-91a3-431d-ad86-5b55eb3ed45f

To Reproduce
I have a complicated setup so won't be able to share a correct way but I've described the error as clearly as possible

Expected behavior

Normally I would expect this to work as any normal FK would.

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

The report names no repository files or tests and does not provide a reproducible setup. Start by reducing the user_roles ForeignKeyField definitions for Application and Role against a manually created schema, then check how source_field="name" is resolved; done means producing a minimal reproduction or confirming the expected behavior with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.