13

I've got an application that was using Grails 1.3.7 which I've just migrated to Grails 2.0. The application makes use of the automatic dateCreated and lastUpdated fields to manage the timestamps associated with creation and modification of the objects. After upgrading, I get the following error:

| Running Grails application
| Error 2012-01-29 22:36:53,504 [Thread-8] ERROR util.JDBCExceptionReporter  - ERROR: null value in column "date_created" violates not-null constraint
| Error 2012-01-29 22:36:53,510 [Thread-8] ERROR events.PatchedDefaultFlushEventListener  - Could not synchronize database state with session

Commenting out the above mentioned fields in my Domain Classes makes the problem go away.

Have the dateCreated and lastUpdated fields been deprecated in Grails 2.0? If so, does that mean that I have to write the code to handle this functionality manually or has the code been moved to a plugin of some sort, like the audit-trail plugin?

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
srkiNZ84
  • 3,558
  • 5
  • 30
  • 37

4 Answers4

18

Ok, fixed it by manually setting the autoTimestamp variable to "true" in the domain class definitions:

static mapping = {
        autoTimestamp true
}

I would guess that this property is not set after migrating a project from Grails 1.3.7 to 2.0.0.

srkiNZ84
  • 3,558
  • 5
  • 30
  • 37
  • thank you so much! I ran into an issue because grails 2.2.0 was overriding the value I was setting in domain class. And more dangerous thing is the timeStamp added by Grails is system clock dependent. – tusar Mar 13 '13 at 10:48
  • I fixed the automatic overriding by setting the autoTimestamp property to `false` – tusar Mar 13 '13 at 10:55
6

Grails 2.0 still supports the automatic timestamps. It's listed in the manual (scroll up a bit from this link).

However, it specifically mentions:

If you put nullable: false constraints on either dateCreated or lastUpdated, your domain instances will fail validation - probably not what you want. Leave constraints off these properties unless you have disabled automatic timestamping.

OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • The link you posted goes to http://localhost/~phil/. Did you put the wrong link in? – srkiNZ84 Jan 29 '12 at 23:41
  • Neither of the fields are mentioned at all in the constraints. Based on the quoted text I'm starting to think that I may have somehow disabled "automatic timestamping". Where did you get that text from? – srkiNZ84 Jan 29 '12 at 23:53
  • Sorry about that, I was browsing my offline copy. The link has been fixed. The warning message is on the page I linked. You have to scroll ***UP*** from the link, because I can't link directly. – OverZealous Jan 30 '12 at 05:51
  • 1
    Ok, fixed it by manually setting the autoTimestamp variable to "true" in the domain class definitions. I would guess that this property is not set after migrating a project from Grails 1.3.7 to 2.0.0. – srkiNZ84 Jan 30 '12 at 08:48
  • I believe Grails 2.X still has autoTimestamp as a default behavior, as OverZealous stated, but I wonder if perhaps there are particular plugins that are overriding it. I have projects that work just fine with this, and yet I've just now seen on of our apps at work start exhibiting this behavior. – Will Buck Apr 09 '13 at 17:23
3

There is a bug in Grails 2.0.3 that can cause this problem when using Postgres. See http://jira.grails.org/browse/GRAILS-8988. The issue says it will resolved when 2.0.4 is released.

Brad Whitaker
  • 1,174
  • 1
  • 8
  • 4
0

I found an alternate solution if you're working in Grails 4:

class ScheduledTaskServiceSpec extends Specification implements ServiceUnitTest<ScheduledTaskService>{

    @Shared @AutoCleanup HibernateDatastore hibernateDatastore
    @Shared AutoTimestampEventListener timestamper

    void setupSpec() {
        hibernateDatastore = new HibernateDatastore(RegistrationCode)
        timestamper = hibernateDatastore.getAutoTimestampEventListener()
    }

    @Transactional
    @Rollback        
    void 'some test method'() {
        when:
        timestamper.withoutDateCreated(MyDomainClass) {
            MyDomainClass mdc = new MyDomainClass(name:"foo")
            mdc.dateCreated = new Date() - 20
        }

        then:
        MyDomainClass.findByName("foo").dateCreated < new Date()
    }
}

Reference

AutoTimestampEventListener

Matt Lachman
  • 4,541
  • 3
  • 30
  • 40