Search code examples
sqlitetimestampormlite

Created at column for ORMLite


A 'modified at' column can be created in ORMLite by using the version annotation, as described at http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#index-time-row-last-modified.

Is it possible to define a 'created at' column, which is automatically populated with the current timestamp but unmodified thereafter?


Solution

  • This works for SQLite:

    @DatabaseField(dataType = DataType.DATE_STRING, columnDefinition = "DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL", readOnly = true, canBeNull = false)
    private Date created_at;
    

    As discussed here, readOnly is required so that ORMLite does not try to insert a row with a null timestamp.