emmett-framework / emmett

The web framework for inventors
BSD 3-Clause "New" or "Revised" License
1.08k stars 72 forks source link

Bug doing put in model whit unique field (version 2.4.5) #428

Closed josejachuf closed 2 years ago

josejachuf commented 2 years ago

I have a model with a unique field, if I make a put (using Emmett-REST) I get an error in which you do not be valid and forces me to change name value


class MyModel(Model):
    tablename = "my_model"

    name = Field.string(length=100, unique=True)
    start = Field.date()
    end = Field.date()

    validation = {
        'name': {'presence': True},
        'start': {'format': '%d/%m/%Y'},
        'end': {'format': '%d/%m/%Y'},       
    }

This fails with these versions:

emmett 2.4.5 emmett-rest 1.4

and works fine whit:

emmett 2.3.1 emmett-rest 1.2.0

josejachuf commented 2 years ago

Error also with

emmett 2.4.5 emmett 2.3.1

gi0baro commented 2 years ago

@josejachuf unfortunately, the unique validator is still based on the pretty-old pyDAL implementation. I already planned some changes for 2.5 in regard of models validations, but with 2.4 there's no easy way support this.

In order to solve your issue in the meantime you have two options:

i) rely on index constraint only, dropping the unique parameter from you field and implementing the validation logic manually on the "controller" side ii) hack the validation in your REST endpoint (I would say in a before_update callback) doing what forms do, eg:

@your_rest_module.before_update
def _fix_unique_validator(row, params):
    current._dbvalidation_record_id_ = row.id

I gonna keep this open until I write down a proper feature change for 2.5

gi0baro commented 2 years ago

I'm closing this in favour of emmett-framework/firestorm#2