emmett-framework / emmett

The web framework for inventors
Other
1.03k stars 70 forks source link

"Invalid Value" error on default datetime field #351

Closed Triquetra closed 3 years ago

Triquetra commented 3 years ago

Given:

class User(Model):
    name = Field()
    updated = Field.datetime()

    default_values = {'updated': lambda: pendulum.now().subtract(weeks=2)}

Attempting: user = User.create(name='Sam')

Yields: <Row {'id': None, 'errors': {'updated': 'Invalid value'}}>

I would have expected the default value to be provided pursuant to the Model definition. However, no value seems to be provided. I tried disabling default validation, but this resulted in a record with a blank 'updated' field. What is wrong here?

Triquetra commented 3 years ago

Specifying "updated=pendulum.now().subtract(weeks=2)" on record creation works. But then setting a default value seems worthless...

gi0baro commented 3 years ago

@Triquetra I'm not able to reproduce your issue.

Tested with the following code:

import datetime
import pendulum
from emmett import App, now
from emmett.orm import Model, Field, Database

class Tmodel(Model):
    a = Field()
    b = Field.datetime()
    c = Field.datetime()
    d = Field.datetime()
    e = Field.datetime()

    default_values = {
        "b": now,
        "c": datetime.datetime.utcnow,
        "d": pendulum.now,
        "e": lambda: pendulum.now().subtract(weeks=2)
    }

app = App(__name__)
db = Database(app)
db.define_models(Tmodel)
giovanni@Deneb ❯ emmett shell
Python 3.8.8 (default, Apr 12 2021, 15:05:24)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Emmett 2.2.2 shell on app: app
>>> Tmodel.create(a="foo")
<Row {'id': 1, 'errors': {}}>
>>> Tmodel.first()
<Row {'id': 1, 'a': 'foo', 'b': datetime.datetime(2021, 6, 14, 23, 19, 16), 'c': datetime.datetime(2021, 6, 14, 23, 19, 16), 'd': datetime.datetime(2021, 6, 15, 1, 19, 16), 'e': datetime.datetime(2021, 6, 1, 1, 19, 16)}>
Triquetra commented 3 years ago

I can confirm that your test code works on my system. Running further tests...

Triquetra commented 3 years ago

I don't know what was wrong. I continued to have problems with my own code, even though the test code worked fine. Out of frustration, I deleted the default values line in my code and retyped it from scratch. Now it works. I must have typed something wrong, and just could not see it...