"Unknown column 'created_at' in 'field list'",


#1

Here is my customer_schema.js file:

up () {                                                                                                                                        
    this.create('customers', (table) => {                                                                                                        
      table.increments()                                                                                                                         
      table.string('name', 30)                                                                                                                   
    })                                                                                                                                           
  }

CustomerSeed.js:

class CustomerSeeder {                                                                                                                           
  async run () {                                                                                                                                 
    const customer = await Factory                                                                                                               
      .model('App/Models/Customer')                                                                                                          
      .create()                                                                                                                              
    console.log('customer: ')                                                                                                                    
  }                                                                                                                                              
}

The Customer.js model is "empty "

I run the migrations, all is Ok, but can not run the seeds : adonis seed throws this error message:

code: 'ER_BAD_FIELD_ERROR',                                                                                                                    
  errno: 1054,                                                                                                                                   
  sqlMessage: "Unknown column 'created_at' in 'field list'",                                                                                     
  sqlState: '42S22',                                                                                                                             
  index: 0,                                                                                                                                      
  sql:                                                                                                                                           
   "insert into `customers` (`created_at`, `name`, `updated_at`) values ('2019-03-04 20:01:17', 'Peter Walsh', '2019-03-04 20:01:17')" }

Why this happens? I did not even declare table.timestamps() in my schema file and :


describe customers;                                                                                                                              
+-------+------------------+------+-----+---------+----------------+                                                                             
| Field | Type             | Null | Key | Default | Extra          |                                                                             
+-------+------------------+------+-----+---------+----------------+                                                                             
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |                                                                             
| name  | varchar(30)      | YES  |     | NULL    |                |                                                                             
+-------+------------------+------+-----+---------+----------------+                                                                             
2 rows in set (0.01 sec)

I do not want to add those 2 fields with table.timestamps() because I do not need them in my database.


#2

Found a solution with a help on Stackoverflow: you have to extend the related model with:

static get createdAtColumn () {
  return null;
}

static get updatedAtColumn () {
  return null;
}

https://adonisjs.com/docs/4.1/lucid#_convention_over_configuration


Unknown column 'created_at' in 'field list'
#3

Hey :wave:

You can also use the NoTimestamp Trait to automate that for you.


#4

Very good to know ! Thank you very much Roman Lanz