Skip to content

Allow "unsetting" ORM model fields #851

@vpetrovykh

Description

@vpetrovykh

Currently we have this notion of "unset" fields in our ORM models. These can be fields that were not fetched in a query or fields that weren't initialized because they have a schema default value. In either case the fields have no value and are not intended to be passed to save().

There is no mechanism that allows a user to "unset" a field once a value has been provided, though.

Consider this schema:

type User {
    required name: str {
        constraint exclusive;
    }
    lucky_number: int64 {
        default := <int64>(100 * random())
    }
}

The lucky_number is both optional and has a default value. So we can create a couple of new users:

...
a = User(name='Alice')
b = User(name='Billie', lucky_number=None)

client.save(a, b)

Alice will have a lucky_number generated and Billie will have no value for that at all. However, there is no way of taking the object b and undoing that lucky_number assignment so that Billie also generates a random lucky_number. Once you have an object's field set to some value, there is no way to undo that short of creating a new object, but that's not always a good strategy (such as when the object has already been referenced somewhere else).

We could allow del to unset a field. This way if we need to modify what information we're going to pass to save(), we can remove fields from an object: del b.lucky_number.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions