Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with inheritance and constraints in Grails/GORM
    primarykey
    data
    text
    <p>Not sure how to frame this question, but I am seeing some behavior that I am unable to explain ... any help will be highly appreciated. I have a super-class <code>Form</code>, and sub-class <code>DraftForm</code>. The super-class has a more stringent constraint on a property <code>content(blank: false)</code> than the sub-class <code>content(nullable: true)</code>, and I am using <code>tablePerHierarchy false</code>. The domain classes are as follows.</p> <pre> class Form { String content static constraints = { content(blank: false) } static mapping = { tablePerHierarchy false } } </pre> <pre> class DraftForm extends Form { static constraints = { content(nullable: true) } } </pre> <p>With the above domain models, the following test * passes * without any problem.</p> <pre> class DraftFormIntegrationSpec extends Specification { void "Testing a draft-form and a form derived from a draft-form"(){ given: "A draft-form with invalid form-fields" // 1 def draftForm = new DraftForm() when: "The draft-form is validated" // 2 assert draftForm.validate() == true then: "The draft-form has no error" // 3 !draftForm.hasErrors() when: "The draft-form is saved" // 4 try{ draftForm.save() }catch(Exception e){ println "Exception thrown!" println e.class } then: "The draft-form is not found in the database" // 5 draftForm.id == null when: "The draft-form is casted to a form" // 6 Form form = (Form) draftForm assert form.validate() == true then: "The form validates, and has no error" // 7 !form.hasErrors() } } </pre> <p>Here are my questions: <li>I think a sub-class also inherits the super-class's constraints. In that case, how come the draft form validates fine and has no error even though the <code>content</code> is <code>null</code> (please see // 2 and // 3 in the test)? <li>If the draft-form validates fine, why do I get an exception (// 4) and cannot find the draft-form in the database (// 5)? <li>When the <code>DraftForm</code> is type casted to a <code>Form</code>, it still validates fine and has no error (// 6 and // 7), even though the <code>content</code> property is still <code>null</code>. How is that possible?</p> <p>Thank you for the help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload