Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on schema generation with several relations #1550

Closed
igiyazov opened this issue Jan 24, 2024 · 2 comments
Closed

Error on schema generation with several relations #1550

igiyazov opened this issue Jan 24, 2024 · 2 comments

Comments

@igiyazov
Copy link

igiyazov commented Jan 24, 2024

I have models:

import uuid
from tortoise import fields, models

class BaseModel(models.Model):
    id = fields.UUIDField(pk=True, editable=False, default=uuid.uuid4)
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        abstract = True

    def __str__(self):
        pass


class User(BaseModel):
    first_name = fields.CharField(max_length=150)
    last_name = fields.CharField(max_length=150)
    username = fields.CharField(max_length=150)
    tg_id = fields.CharField(max_length=150)


class Check(BaseModel):
    user = fields.ForeignKeyField(
        'models.User', related_name='checks', null=True, on_delete=fields.SET_NULL, default=None, to_field="id"
    )
    lg = fields.CharField(max_length=150, unique=True)


class Product(BaseModel):
    name = fields.CharField(max_length=150)
    count = fields.IntField()
    check = fields.ForeignKeyField(
        'models.Check', related_name='products', null=True, on_delete=fields.SET_NULL, default=None
    )

Code above issue:
Снимок экрана 2024-01-24 в 14 57 33

And when I change name of field check in Product model to any other, everything work.

E.g.:
bill = fields.ForeignKeyField( 'models.Check', related_name='products', null=True, on_delete=fields.SET_NULL, default=None )

What's wrong I do?

@vlakius
Copy link

vlakius commented Apr 24, 2024

I speculate that it may be a collision of the field name 'check' with a method of the model bearing the same name.

This has happened to me in the past and I have not found a better solution other than to change the field name or prefix it with a _ (undescore)

EDIT:
related to #1559

@abondar
Copy link
Member

abondar commented May 24, 2024

Your example should work in 0.21.0 as check is not reserved anymore

@abondar abondar closed this as completed May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants