tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.58k stars 378 forks source link

Error on schema generation with several relations #1550

Closed igiyazov closed 4 months ago

igiyazov commented 8 months ago

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 commented 5 months ago

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 commented 4 months ago

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