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

Can't use check field name #1559

Closed
Arteemoon opened this issue Feb 25, 2024 · 4 comments
Closed

Can't use check field name #1559

Arteemoon opened this issue Feb 25, 2024 · 4 comments

Comments

@Arteemoon
Copy link

from enum import Enum
from tortoise import models, fields

from models.users import User

class PaymentTypeEnum(Enum):
    CASH = 'cash'
    CASHLESS = 'cashless'

class Check(models.Model):
    id = fields.IntField(pk=True)
    created_by: fields.ForeignKeyRelation[User] = fields.ForeignKeyField('models.User', null=False)
    payment_type = fields.CharEnumField(enum_type=PaymentTypeEnum)
    payment_amount = fields.DecimalField(max_digits=8, decimal_places=2)
    created_at = fields.DatetimeField(auto_now_add=True)


class Product(models.Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=30)
    price = fields.DecimalField(max_digits=8, decimal_places=2)
    quantity = fields.IntField()
    check: fields.ForeignKeyRelation[Check] = fields.ForeignKeyField(Check, null=False, related_name='products')

this is my code, when i try run aerich migrate

Get this error

File "/usr/local/lib/python3.10/site-packages/tortoise/backends/base/schema_generator.py", line 185, in _get_table_sql self._get_models_to_create(models_to_create) File "/usr/local/lib/python3.10/site-packages/tortoise/backends/base/schema_generator.py", line 412, in _get_models_to_create model.check() TypeError: 'property' object is not callable

@lucasbrahm
Copy link
Contributor

lucasbrahm commented Feb 28, 2024

CharEnumField expects an instance of type str, so you could modify your enum to:

class PaymentTypeEnum(str, Enum): CASH = 'cash' CASHLESS = 'cashless'
Check some example here: https://tortoise.github.io/examples/basic.html?h=charenumfield#enumeration-fields

@vlakius
Copy link

vlakius commented Apr 26, 2024

what @lucasbrahm said is true but i think that the problem here is a name collision with the field "check" that collide with the method "check" of the models.
If i'm right it could be a good idea to rename the internal methods with something like _check to avoid name collision
EDIT:
this is the classmethod causing the problem:
https://github.com/tortoise/tortoise-orm/blob/5c8d81c554f210a191a36f373c6a66a2d9943412/tortoise/models.py#L1369C1-L1375C42

@abondar
Copy link
Member

abondar commented Apr 26, 2024

Yeah, check being reserved seems like bad decision :)
I'll look into renaming it

@abondar
Copy link
Member

abondar commented May 24, 2024

In 0.21.0 you should be able to use check field name

@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

4 participants