I have the following django data model
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)
It ends up with database
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
Since, this table will also be accessed by non-django app. I need to make sure the auto timestamp generation (For column ts) is fully handled by postgres, not python.
I don't expect to have
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
------------+--------------------------+-----------+----------+---------+---------+--------------+-------------
ts | timestamp with time zone | | not null | | plain | |
I expect to have
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
------------+--------------------------+-----------+----------+---------+---------+--------------+-------------
ts | timestamp with time zone | | not null | now() | plain | |
I had tried other technique like auto_now=True, auto_now_add=True, ...
But, none of them generate the table schema I want. By using any below, the generated table schema, its default column is still empty.
default=timezone.nowauto_now=Trueauto_now_add=True
defaults are not filled in by the database, but by Django.