[英]How to give a default value in adonis model. i am using adonis typescript
Here i am using adonis framework of node.js in which it uses their own orm (lucid orm).在这里,我使用 node.js 的 adonis 框架,其中它使用自己的 orm (lucid orm)。 I am stuck how to give a default value here for a date i using a publishedDate field in code.I want to store a current date in YYYY-MM-DD format我被困在这里如何为我在代码中使用publishedDate字段的日期提供默认值。我想以YYYY-MM-DD格式存储当前日期
import { DateTime } from 'luxon'
import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm'
import User from './User'
export default class Post extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public title: string
@column()
public description: string | null
@column()
public publishedDate: Date
@column()
public userId: number
@column.dateTime({ autoCreate: true })
public createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
@belongsTo(() => User, {
foreignKey: 'userId',
})
public posts: BelongsTo<typeof User>
}
You can assign default values in your table migrations, example您可以在表迁移中分配默认值,例如
table.date('published_date').defaultTo(#YOUR DATE#)
However, I'm not sure if you can store it in YYYY-MM-DD format.但是,我不确定您是否可以以 YYYY-MM-DD 格式存储它。 You can play around and see if it works.你可以玩一下,看看它是否有效。 If it does not work, then you have to store your date as ISO datetime string and use the @computed
decorator to format your date.如果它不起作用,那么您必须将日期存储为 ISO 日期时间字符串并使用@computed
装饰器来格式化您的日期。
https://docs.adonisjs.com/reference/orm/decorators#computed https://docs.adonisjs.com/reference/orm/decorators#computed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.