尽管使用了DateTimeField,但无法设置默认时间戳(默认= datetime.datetime.now) 该列设置为非null但未设置默认值
我有我的模特
import datetime
database = PostgresqlDatabase(dbname,username,password,host)
class BaseModel(Model):
class Meta:
database = database
class UserInfo(BaseModel):
id = PrimaryKeyField()
username = CharField(unique=True)
password = CharField()
email = CharField(null=True)
created_date = DateTimeField(default=datetime.datetime.now)
当我使用此模型和下面的代码
创建表格时 database.connect()
database.create_tables([UserInfo])
我在桌子下面
Table "public.userinfo" Column | Type | Modifiers --------------+-----------------------------+------------------------- ------------------------------ id | integer | not null default nextval('userinfo_id_seq'::regclass) username | character varying(255) | not null password | character varying(255) | not null email | character varying(255) | created_date | timestamp without time zone | not null Indexes: "userinfo_pkey" PRIMARY KEY, btree (id) "userinfo_username" UNIQUE, btree (username)
此处表格中创建的日期并未设置为任何默认
答案 0 :(得分:2)
尝试created_date = DateTimeField(constraints=[SQL('DEFAULT CURRENT_TIMESTAMP')])