Removed timestamps in migrations, but it keeps trying to write

I have removed the timestamps “created_at” and “updated_at” from my database migrations, and updated to:

this.create('tokens', (table) => {
      table.increments()
      table.integer('user_id').unsigned().references('id').inTable('users')
      table.string('token', 255).notNullable().unique().index()
      table.string('type', 80).notNullable()
      table.timestamp('token_created_at')

and my function:

async requestNewPassword({ request, response }) {
    const { email } = request.only(["email"]); //PEGA O EMAIL DA REQUEST
    const user_id = await Database.from("users").where("email", email);

    // GERA O TOKEN PARA RECUPERACAO E GRAVA NO BANCO
    const forgotToken = crypto.randomBytes(10).toString("hex");
    const tokenData = {
        user_id: user_id,
        token: forgotToken,
        type: "forgotToken",
        token_created_at: new Date()
    }

    const token = await Token.create(tokenData);
    return token;
  }

But i keeps tying to write on created_at and updated_at. Someone can help me please?

1 Like

Hi, welcome to the community :wave:

.timestamp() : documentation
^ Adds created_at and updated_at columns on the database

You can use : .datetime() or .time() for your case
https://knexjs.org/#Schema-datetime
https://knexjs.org/#Schema-time

Have a nice day!

1 Like

Keeps the same error

Remove .timestamp()

or

Use .dropTimestamps() > documentation

I think you should also inform the related model that you do not need the timestapms. One possible sollution is to add the following static methods into that model:

 static get createdAtColumn () {
   return null
 }

 static get updatedAtColumn () {
   return null
 }
2 Likes

Just want to extend @begueradj answer:

There is a NoTimestamp trait included in adonis-api-app boilerplate.

/Models/Traits/NoTimestamp.js code:

'use strict'

class NoTimestamp {
  register (Model) {
    Object.defineProperties(Model, {
      createdAtColumn: {
        get: () => null,
      },
      updatedAtColumn: {
        get: () => null,
      }
    })
  }
}

module.exports = NoTimestamp

Usage in a model file:

const Model = use('Model')

class Token extends Model {
  static boot () {
    super.boot()
    this.addTrait('NoTimestamp')
  }
}

source

2 Likes