2

I have created basic schema with timestamps, but when I insert into table in seeder, column created_at and updated_at are null, but based on the Knex.js documentation I thought it should be current datetime if not specified.

Latest adonis libraries, database: mysql 5.7.

My Schema

'use strict'

const Schema = use('Schema')

class UserSchema extends Schema {
  up () {
    this.create('users', (table) => {
      table.increments()
      table.string('name')
      table.timestamps()
    })
  }

  down () {
    this.drop('users')
  }
}

module.exports = UserSchema

My Seeder

'use strict'

const Factory = use('Factory')
const Database = use('Database')

class UserSeeder {
  async run () {
    await Database.table('users').insert({
      name: 'JP',
    })
  }
}

module.exports = UserSeeder
Tomas Bruckner
  • 718
  • 2
  • 10
  • 22

3 Answers3

3

Timestamps only works on LUCID ORM. You are using direct database. Set your json in the fields, it will work:

await Database.table('users').insert({
   name: 'JP',
   created_at,: Database.fn.now(),
   updated_at : Database.fn.now()
})
2

I have found that timestamps are default only when using models, but not when using database directly. This one is now covered in the documentation. Would be great if it was default on database level instead.

Tomas Bruckner
  • 718
  • 2
  • 10
  • 22
1

You should add the timestamps arguments like useTimestampType and makeDefaultNow:

table.timestamps(true, true)