1

I am using below code to put my row details through post request in db but error is coming:

(insert into `product` (`created_at`, `description`, `id`, `image`,`pages`, `title`, `updated_at`) 
values ('2021-02-14 02:44:52', 'sddsds', 12, 'image', '23', 's', '2021-02-14 02:44:52') - 
ER_BAD_FIELD_ERROR: Unknown column 'created_at' in 'field list').

I am hitting /products/ with body :

 {
    "productId": 12,
    "description": "sddsds",
    "title": "s",
    "image":"image",
    "pages": "23"
}

The route :

Route.resource("products", "ProductsController");

My Product controller :

async store({ request, response }: HttpContextContract) {
    const { productId, description, title, image, pages } = request.post()
 
    // save and get instance back
    const product = await Product.create({ productId, description, title, image, pages })
   
    response.status(201).json({
      message: 'Successfully created a new product.',
      data: product
    })
  } 

My Product model :

import { DateTime } from 'luxon'
import { BaseModel, column, computed, hasMany, HasMany } from '@ioc:Adonis/Lucid/Orm'
import Price from './Price'

export default class Product extends BaseModel {
  public static table = 'product'

  @column({ isPrimary: true, columnName: 'id', serializeAs: 'productId' })
  public productId: number

  @column({ columnName: 'title' })
  public name: string

  @column({ serialize: (v) => (v ? v : undefined) })
  public description: string

  @column({ serialize: (v) => (v ? v : undefined) })
  public image: string

  @column({ serialize: (v) => (v ? v : undefined) })
  public pages: string

  @column.dateTime({ autoCreate: true })
  public createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  public updatedAt: DateTime

  @hasMany(() => Price, {
    foreignKey: 'productId',
    onQuery: (query) => query.whereNull('deleted_at'),
    serializeAs: null,
  })
  public prices: HasMany<typeof Price>

  @computed({ serializeAs: 'price' })
  public get price() {
    if (this.prices && this.prices.length) {
      return this.prices[0].originalPrice
    }

    return 0
  }

  @computed()
  public get quantityInCart() {
    return this.$extras.pivot_quantity
  }
}
crbast
  • 1,750
  • 1
  • 7
  • 16

0 Answers0