Search code examples
ormcfmllucee

Ignored default value in Lucee Orm


I set my persistent component with this property:

<cfproperty name="active" ormType="timestamp" notnull="true" dbDefault="now()" />

Now, if I save an entity by not specifying its created_at value, I get an error: not-null property references a null or transient value: User.active.

How can I skip specifying all the columns when creating entities?

Thanks!


Solution

  • Rather than setting defaults in the database schema, I would define them in your entity's properties using the default attribute to avoid null values.

    However, bear in mind that only simple and non-dynamic values - such as fixed strings and numbers - can be defined as defaults. If you need to define a complex value, like an array, or a dynamic one, such as Now(), you'll need to set those in the entity's init() method.

    component name="user" accessors=true persistent=true{
    
     property name="active" ormtype="boolean" default=false;
     property name="created_at" ormtype="timestamp";
    
     function init(){
      variables.created_at=Now();
      return this;
     }
    
    }