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

utils.arg_to_sql function can not be redeclared in Field subclass #209

Open
M1ha-Shvn opened this issue Jan 12, 2024 · 0 comments
Open

utils.arg_to_sql function can not be redeclared in Field subclass #209

M1ha-Shvn opened this issue Jan 12, 2024 · 0 comments

Comments

@M1ha-Shvn
Copy link
Contributor

Hi.
I want to implement a small wrapper around StringField which automatically dumps and loads JSON and stores it in ClickHouse as a string. Here is my code:

class JSONField(StringField):
    class_default = {}

    def to_python(self, value, timezone_in_use):
        if isinstance(value, (list, dict, int, float, bool)):
            return value

        result = super().to_python(value, timezone_in_use)
        return json.loads(result) if result is not None else None

    def validate(self, value):
        if value is not None and not isinstance(value, (int, float, bool, str, list, dict)):
            raise ValueError(f"{self.__class__.__name__} is not valid JSON type - {value}")

    def to_db_string(self, value, quote=True):
        if isinstance(value, (list, dict)):
            value = json.dumps(value)

        return super().to_db_string(value, quote=quote)

But when I use a QuerySet.update(...) frunction trying to update this field I get an error:

infi.clickhouse_orm.database.ServerError: There is no supertype for types Array(String), String because some of them are Array and some of them are not

This is caused by clickhouse_orm utils.arg_to_sql which determines argument type based on its form and there is no any ability to add functionality there using Field child class methods.

I suggest get suitable field from model for every updated value. Than call Field.to_db_string(...) method to convert field value instead of using arg_to_sql function.

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

1 participant