【问题标题】:Peewee Python Default is not reflecting in DateTimeFieldPeewee Python Default 未反映在 DateTimeField 中
【发布时间】:2016-10-27 21:02:24
【问题描述】:

尽管使用 DateTimeField(default=datetime.datetime.now),但无法设置默认时间戳 该列设置为非空但未设置默认值

我有我的模型

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)

此处的表格创建日期未设置为任何默认值

【问题讨论】:

    标签: postgresql peewee flask-peewee


    【解决方案1】:

    【讨论】:

    • 这太疯狂了。如果您要执行“python 默认”,请不要悄悄地覆盖完善的行为语义。称它为 python_default 或其他名称,并明确说明您的 ORM 实际上并不支持真正的数据库默认值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2022-10-13
    • 2017-10-06
    • 2020-04-10
    • 2016-04-05
    • 2014-04-27
    相关资源
    最近更新 更多