【问题标题】:How to ensure Django generate postgres table schema for "timestamp with time zone" with default "now()"如何确保 Django 使用默认的“now()”为“带时区的时间戳”生成 postgres 表模式
【发布时间】:2018-08-25 08:27:37
【问题描述】:

我有以下 django 数据模型

class ApiLog(models.Model):
    name = models.TextField(blank=False, null=False)
    ts = models.DateTimeField(default=timezone.now, blank=False, null=False)
    ip_address = models.GenericIPAddressField(blank=True, null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

以数据库结束

django=# \d+ users_apilog
                                                           Table "public.users_apilog"
   Column   |           Type           | Collation | Nullable |                 Default                  | Storage  | Stats target | Description
------------+--------------------------+-----------+----------+------------------------------------------+----------+--------------+-------------
 id         | integer                  |           | not null | nextval('users_apilog_id_seq'::regclass) | plain    |              |
 name       | text                     |           | not null |                                          | extended |              |
 ts         | timestamp with time zone |           | not null |                                          | plain    |              |
 ip_address | inet                     |           |          |                                          | main     |              |
 user_id    | integer                  |           | not null |                                          | plain    |              |
Indexes:
    "users_apilog_pkey" PRIMARY KEY, btree (id)
    "users_apilog_user_id_2eb2b1cf" btree (user_id)
Foreign-key constraints:
    "users_apilog_user_id_2eb2b1cf_fk_users_user_id" FOREIGN KEY (user_id) REFERENCES users_user(id) DEFERRABLE INITIALLY DEFERRED

因此,非 django 应用程序也将访问此表。我需要确保自动时间戳生成(对于列 ts)完全由 postgres 处理,而不是 python。


我不希望有

   Column   |           Type           | Collation | Nullable | Default | Storage | Stats target | Description
------------+--------------------------+-----------+----------+---------+---------+--------------+-------------
 ts         | timestamp with time zone |           | not null |         | plain   |              |

我希望有

   Column   |           Type           | Collation | Nullable | Default | Storage | Stats target | Description
------------+--------------------------+-----------+----------+---------+---------+--------------+-------------
 ts         | timestamp with time zone |           | not null | now()   | plain   |              |

我曾尝试过其他技术,例如 auto_now=True、auto_now_add=True、...

但是,它们都没有生成我想要的表模式。通过使用以下任何生成的表架构,其default 列仍然为空。

  • default=timezone.now
  • auto_now=True
  • auto_now_add=True

【问题讨论】:

  • defaults 不是由数据库填写,而是由 Django 填写。
  • @WillemVanOnsem 是的。我知道。我想让它由数据库填充。有什么办法吗?
  • @CheokYanCheng:那是不是 BearBrown,BearBrown实际上发布了一个sensical副本。

标签: python django postgresql


【解决方案1】:

因为,如今使用 docker 非常普遍。我演示了我是如何解决它的,基于 docker 开发环境。

第 0 步:确保您已经提交了生成的 0001_initial.py

第一步:生成空迁移文件

docker-compose run --rm -v %cd%/django:/app -w /app django /app/manage.py makemigrations users --empty -n alter_ts_default_to_now

第 2 步:在迁移文件中使用原始 SQL

# Generated by Django 2.1 on 2018-08-25 09:27

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL(
            "ALTER TABLE users_apilog ALTER COLUMN ts SET DEFAULT now()",
        )    
    ]

第 3 步:再次运行迁移

#!/bin/sh

python manage.py makemigrations users

python manage.py makemigrations

python manage.py migrate

echo "yes" | python manage.py collectstatic

exec "$@"

【讨论】:

    猜你喜欢
    • 2022-11-22
    • 2015-10-07
    • 2016-04-07
    • 1970-01-01
    • 2020-06-15
    • 2011-12-27
    • 1970-01-01
    • 2012-11-30
    • 2015-11-19
    相关资源
    最近更新 更多