python - Django datetime primary key doesn't constrain unique -
so, here's model:
class mymodel(models.model): timestamp = models.datetimefield(primary_key=true) fielda = models.textfield()
when call syncdb
, django doesn't add constraint postgresql
timestamp unique, though gives primary key constraint.
if change timestamp just unique=true
, django creates unique constraint. however, if combine unique=true
, primary_key=true
, django doesn't create unique constraint.
my info:
- django-south v1.8
- django 1.4
- python 2.7
- postgresql 9.1
edit:
if save model same exact timestamp twice in 2 different runs (not same process), doesn't raise , integrityerror
should. when creates unique constraint , no primary key, same code.
edit 2:
this code runs when have constraint "mymodel_pkey" primary key ("timestamp")
in postgresql having timestamp = models.datetimefield(primary_key=true)
timestamp = datetime.datetime(2013, 7, 31, 0, 0, 0).replace(tzinfo=utc) print timestamp # prints 2013-07-31 00:00:00+00:00 row = mymodel(timestamp=timestamp, fielda='test') row.save()
when run twice in row, doesn't raise integrityerror
. however, unique=true
, not primary_key=true
, does raise integrityerror
.
maybe example docs can bring light question
technically, primary key constraint combination of unique constraint , not-null constraint. so, following 2 table definitions accept same data:
create table products ( product_no integer unique not null, name text, price numeric ); create table products ( product_no integer primary key, name text, price numeric );
[...] primary key indicates column or group of columns can used unique identifier rows in table [...] adding primary key automatically create unique btree index on column or group of columns used in primary key. [...]
Comments
Post a Comment