Open Trebla7th opened 4 years ago
Sorry for some of the extraneous things (like the type enum)... took me a while to figure out what was actually triggering the error.
I have also run into this bug while attempting to upgrade a grails 2.5 app to 4.0.3. Is there a work around at all?
I am surprised that there's not more comments on this one, seems like it would affect a lot of people. We have sold our management on a Grails 4 upgrade, please don't let us down Grails!
I see two areas where I dont think GORM features are being used properly.
The constraint for unique:'dogHouse'
is improperly used. By grails documentation, the value should be false or true. Here the field value is given. In order to define it correctly, it can be written as dogHouse(unique: true)
. When I alter the sample application with this change, it no longer gives an exception. Also I confirmed if you take out the unique constraint, the exception also goes away. With these two facts, it seems to me that the usage of it in the sample app is wrong.
With an inheritance hierarchy for a PetObject
and Dog
, currently PetObject
resides in the domain package. I turned on logSql with hibernate and the created table is pet_object. This indicates that that the PetObject
is the class that is scanned to map. In addition, the unique constraint is the DSL is not translated to the database ddl. I believe the proper way is to move the PetObject
to the groovy folder and leave Dog
in domain. When I do this in the sample application, the dog table is correctly created in ddl, and all the constraints above are translated into ddl constraints.
As for the fact that it worked in Grails 3, this may be a consequence of hibernate and gorm not being as "smart" in previous versions, and now it requires a more clear definition of terms and placement of domain heirarchy.
@puneetbehl please review the above and determine what we should do thanks
I believe (1) is an incorrect interpretation. dogHouse should not be unique, but no two dogs can have the same name in the same doghouse. For example there can be "Rover" in dogHouse1 and "Rover" in dogHouse2. Perhaps this changed since grails 3, this application runs fine in grails 3.
For (2), I don't believe this is a constraint on the database, this is a grails constraint on the data model enforcing uniqueness within a parent object.
Again, this is a regression from grails 3 functionality (or possibly a change that I've misinterpreted). I am not in a location I can test your changes against samples to see if the resolve the issue until the end of next week.
Edit: Sorry, I'm rushing and didn't notice your comment about gorm/hibernate not being as smart. This is certainly possible, but I believe it's still a regression from documented functional intent.
According to the grails docs - https://docs.grails.org/latest/ref/Constraints/unique.html
Uniqueness can be specified as in the example
You can also define multi-column unique constraints by declaring the other field(s) to be included as the parameter value. If there is one other field, specify its name, but if there are more than one use a List, for example:
Example:
group unique: 'department'
In this example the group name must be unique in one department but there might be groups with same name in different departments, i.e. the group name isn’t unique by itself.
When a domain hierarchy exists, having a constraint on the child class that checks a field of the child class will cause a grails error
Property [xxx] is not a valid property of class example.<parentObject>
when saving the child object.This only occurs when the constraint is present, in the referenced example, if you remove the "unique" constraint on the child objects, they can be saved just fine. This worked in grails 3.
The error claims to come from the abstract parent object's validation, that the field does not exist on the object, even though it does exist on the concrete child object which was instantiated and saved.
Task List
Steps to Reproduce
Remove the constraints from Cat and Dog classes, restart application and repeat steps, the error does not occur
In the attached application,
PetObject
is abstract,Dog
extendsPetObject
and has an additional property that it belongs to calleddogHouse
Dog class:
When attempting to create the object as follows
The exception is thrown
Stack Trace:
Expected Behaviour
Domain class should validate and save
Actual Behaviour
Error above is thrown
Environment Information
Operating System: MacOS Mojave Version 10.14.6
Grails Version: 4.0.3
JDK Version: OpenJDK Runtime Environment (Zulu 8.33.0.1-macosx) (build 1.8.0_192-b01) OpenJDK 64-Bit Server VM (Zulu 8.33.0.1-macosx) (build 25.192-b01, mixed mode)
Container Version (If Applicable): Tested on both Tomcat and Undertow, same error on both
Example Application
https://github.com/Trebla7th/abstract-domain-validation-error