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

Model .update() does not check cast attributes #759

Closed
circulon opened this issue Jul 13, 2022 · 3 comments
Closed

Model .update() does not check cast attributes #759

circulon opened this issue Jul 13, 2022 · 3 comments
Labels
bug An existing feature is not working as intended

Comments

@circulon
Copy link
Contributor

circulon commented Jul 13, 2022

Describe the bug
using .update() on a model with attribute changes that have to be cast does not work.
It looks like all values are passed directly to the QueryBuilder without being transformed in any way.

To Reproduce

class User(Model):
     __casts__ = {"roles": "json"}

    __fillable__ = [
        "name",
        "age"
        "roles",
    ]

user = User.create({
    "name": "Bob",
    "age": 26,
    "roles": ["admin"],
}, casts=True).fresh()

# now we want to update the model
user.update({
  "roles": ["std_user"],
})

this fails in postgres with an error similar to the following

column "roles" is of type json but expression is of type text[]
LINE 1: ..."roles" = ARRAY['adm...
                              ^
HINT:  You will need to rewrite or cast the expression.  

Current workaround is to update each attribute on the model separately and use .save() which casts the attributes to their intended column type correctly.
eg

user.name = "Bobson"
user.roles = ["std_user"]
user.save()

But this is just less clean if many attributes are required for update.

Expected behavior
I should be able to use .update() on a model with attributes that are in the casts list

Desktop (please complete the following information):

  • OS: Mac OSX
  • Version Monterey 12.4

What database are you using?

  • Type: Postgres
  • Version 10.5
  • Masonite ORM 2.18.0
@circulon circulon added the bug An existing feature is not working as intended label Jul 13, 2022
@BSN4
Copy link
Contributor

BSN4 commented Jul 13, 2022

related #755

@josephmancuso
Copy link
Member

This presents a potential breaking change. I'll have to just verify

@josephmancuso
Copy link
Member

Since this is a breaking change i put this in the 3.x branch. Release date should be around the beginning of september

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
None yet
Development

No branches or pull requests

3 participants