-
I create a price field on migration and defined it as number on model, but when it's serialized, returns a string. export default class Provides extends BaseSchema {
protected tableName = 'provides'
public async up(): Promise<void> {
this.schema.createTable(this.tableName, (table) => {
table.uuid('id').primary()
...
table.decimal('price', 12, 2)
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })
})
}
public async down(): Promise<void> {
this.schema.dropTable(this.tableName)
}
} export default class Provide extends BaseModel {
public static selfAssignPrimaryKey = true
@column({ isPrimary: true })
public id: string
...
@column()
public price: number
...
@column.dateTime({ autoCreate: true })
public createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
@beforeCreate()
public static assignUuid(provide: Provide): void {
provide.id = uuid()
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Some database drivers (Postgres, for example) will return decimals as strings due to possible loss of precision with floating point numbers. If you're using Postgres, we've worked around this by adding the following to the register function of the app's AppProvider. It will convert the strings to numbers before they hit the Adonis framework. If you're not using Postgres, your mileage may vary, but hopefully this sends you in the right direction.
|
Beta Was this translation helpful? Give feedback.
-
In import pg from 'pg' and call to pg types: pg.types.setTypeParser(20, 'text', parseInt) |
Beta Was this translation helpful? Give feedback.
Some database drivers (Postgres, for example) will return decimals as strings due to possible loss of precision with floating point numbers. If you're using Postgres, we've worked around this by adding the following to the register function of the app's AppProvider. It will convert the strings to numbers before they hit the Adonis framework. If you're not using Postgres, your mileage may vary, but hopefully this sends you in the right direction.