0

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!

Manaus
  • 431
  • 5
  • 17
  • The error seems to relate to a property called `active` rather than `created_at`. Can you post a bit more code to clarify what you're trying to do? – CfSimplicity Dec 27 '15 at 20:25
  • Thanks, you're right this is the code ``, when adding the entity I do `u = entityNew('user'); u.setEmail('mymail@domanin.com'); entitySave(u);`, the Db structure (Postgresql) seems ok, `ALTER TABLE users ADD COLUMN active boolean; ALTER TABLE users ALTER COLUMN active SET NOT NULL; ALTER TABLE users ALTER COLUMN active SET DEFAULT false;` – Manaus Dec 27 '15 at 20:30
  • OK, makes more sense. Have you tried using `default=false` rather than, or in addition to `dbdefault`. That should ensure the property does not have a null value when the entity is saved. – CfSimplicity Dec 27 '15 at 22:14
  • Also, for posterity, it would be best if you could edit your question so it shows the property you're asking about (ie `active` rather than `created_at`). – CfSimplicity Dec 27 '15 at 22:16

1 Answers1

0

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;
 }

}
CfSimplicity
  • 2,338
  • 15
  • 17