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

Can't unset value on embedded object #718

Closed
paulfairless opened this issue May 23, 2016 · 17 comments
Closed

Can't unset value on embedded object #718

paulfairless opened this issue May 23, 2016 · 17 comments
Assignees
Milestone

Comments

@paulfairless
Copy link

Encountered an issue while upgrading a grails 2.5.4 app to Gorm 5 mongodb:5.0.6.RELEASE, whereby I can no longer unset a values from within an embedded object list. It seems null values are ignored as setting to a different value works as expected:

Owner.groovy

class Owner {
    String name

    List<Pet> pets
    static embedded = ['pets']
}

Pet.groovy

class Pet {
    String name
    static constraints = {
        name nullable:true
    }
}

Bootstrap.groovy

        Owner o = new Owner(name:"bob", pets:[new Pet(name:"fido")])
        p.save(flush:true)


        Owner.withNewSession {
            Owner o2 = Owner.findByName("bob")
            assert o2.pets[0].name == "fido"

            o2.pets[0].name = null
            o2.save(flush:true)
            assert !o2.hasErrors()
            assert o2.pets[0].name == null
        }

        Owner.withNewSession {
            Owner o3 = Owner.findByName("bob")
            assert o3.pets[0].name == null
        }
@graemerocher
Copy link
Member

Try o2.markDirty('pets') then call save(..)

@paulfairless
Copy link
Author

no luck I'm afraid. In my real world app the save includes changes to other properties in the embedded object, which all work fine. It's just the null value seems to be ignored

@paulfairless
Copy link
Author

Hello,

Just trying to test this update with the 5.0.8 release. It seems to be fixed in grails 2.5.4, but not grails 3.1.x?

Thanks
Paul

@graemerocher
Copy link
Member

Should be fixed in both, other please upload a 3.1.x app somewhere that reproduces the problem

@paulfairless
Copy link
Author

https://github.com/paulfairless/myapp

Tried both 6.0.0.M1 and 5.0.8

@graemerocher
Copy link
Member

Thanks will take a look next week, underlying issue is probably the property not being marked as dirty since nulls weren't taken into account before. Calling obj.markDirty("myProp") would probably work around it

@paulfairless
Copy link
Author

Thanks Graeme, appreciated. Not having any luck with markDirty. Given it's working with the same sample app in 2.5.4 is it possible that the changes didn't get released via the 3.1.x build process correctly?

@paulfairless
Copy link
Author

looks like this works in grails 3.1.8 but didn't in 3.1.6

@graemerocher
Copy link
Member

Probably for 3.1.6 you didn't have the correct libraries on your path. See http://grails.io/post/143431711043/how-to-use-a-specific-version-of-gorm-in-grails-3 for how to use a specific version of GORM

@paulfairless
Copy link
Author

yes - I expect that was my issue. Many thanks

@ghost
Copy link

ghost commented Dec 9, 2016

Can someone please tell how to fix this issue? I have an embedded object in my domain class which once set, doesn't get unset. I used Grails 3.1.6 as well as 3.1.8 along with various versions of GORM including 5.0.6.RELEASE and upwards till 5.0.12.RELEASE.
I changed the version as mentioned in above comment: http://grails.io/post/143431711043/how-to-use-a-specific-version-of-gorm-in-grails-3
Same issue persists with all the combinations. Oh, and, markDirty also didn't help.

@ghost
Copy link

ghost commented Dec 20, 2016

Can someone please comment on how to fix this issue? I am still not able to figure out how to fix this issue

@injecteer
Copy link

I created another issue grails/gorm-mongodb#152
Also you will find a workaround there

@AbhishekTaparia
Copy link

I am also facing this issue

Configurations:
Grails - 4.0.11
GORM - 7.0.8.RELEASE

I want unset the value of a string field token but it is not persisting to database.

domainInstance.token = null
// domainInstance.token.putAt("token", null) 
domainInstance.markDirty("token")
domainInstance.save(flush: true, failOnError: true)

I have made the token field nullable.

static constraints = {
    token nullable:true
}

Although when I try this will non-null value, it is getting updated.
Also with lower-level APIs it is working, but I don't want to use those.

@sagrawal31
Copy link

Can we reopen this issue, please

@taplar
Copy link

taplar commented Feb 16, 2022

Having the same issue.

Grails: 4.0.1
Gorm: 7.0.2.RELEASE

Setting a List<> to null on one of the domain objects, the property was not being removed from the persisted object. The other non-list changes were being persisted. I was able to remove it with the a Domain.collection.updateOne call, but I ran into issues with being able to rollback the changes inside a transaction when intermixing updates like that with normal Gorm updates.

One thing that does seem to work for me is to call trackChanges on the domain before making any changes. This results in the property removal being persisted to the database. I noticed that the isDirty check was returning true, but the call to get the dirty property names was null, so viewing the DirtyCheckable I saw that the $changedProperties was used all over the place and was null in our use case. Not entirely sure how the property was considered dirty, but not persisted, but making that Map not be null changes things, but at this point I'll take it.

@sagrawal31
Copy link

Any update here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants