0

So, here's my model:

class MyModel(models.Model):
    timestamp = models.DateTimeField(primary_key=True)
    fielda = models.TextField()

When I call syncdb, django doesn't add a constraint to postgresql for timestamp to be unique, even though it gives it the primary key constraint.

If I change timestamp to just unique=True, django creates the unique constraint. However, if I combine unique=True and primary_key=True, django doesn't create the unique constraint.

My info:

  • django-south v1.8
  • django 1.4
  • python 2.7
  • postgresql 9.1

Edit:

If I save a model with the same exact timestamp twice in two different runs (not the same process), it doesn't raise and IntegrityError like it should. But when it creates the unique constraint and no primary key, it does with the same code.

Edit 2:

This is the code that runs when I have CONSTRAINT "MyModel_pkey" PRIMARY KEY ("timestamp") in postgresql from just 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 a row, it doesn't raise an IntegrityError. However, with the unique=True and not primary_key=True, it does raise an IntegrityError.

Community
  • 1
  • 1
notbad.jpeg
  • 3,308
  • 1
  • 32
  • 37
  • Surely a primary key always implies a unique constraint? You can't have duplicate primary keys. – Daniel Roseman Jul 31 '13 at 09:00
  • For me, this is not a problem: your database has the constraint "PRIMARY KEY" on the field "timestamp", you don't need an additional constraint "UNIQUE" on that same field, the primary key constraint does the job. For example, if you look your other tables, there is no "UNIQUE" constraint on the auto "id" fields generated by Django as primary keys. – Ricola3D Jul 31 '13 at 09:03
  • I edited my question in regards to your comments. – notbad.jpeg Jul 31 '13 at 09:16
  • Are you sure it's the same timestamp? Is it exact to the microsecond because if it isn't, it's of course not unique. – DrColossos Jul 31 '13 at 09:22
  • I edited the question again. – notbad.jpeg Jul 31 '13 at 09:29
  • Ah okay. Here is the same issue, but I think the only solution is manualy checking the existence if you don't use a Autofield.. http://stackoverflow.com/questions/6039443/primary-key-and-unique-key-in-django – Ricola3D Jul 31 '13 at 10:56

2 Answers2

1

Maybe the example from the docs can bring some light into your question

Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint. So, the following two table definitions accept the 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 );

[...] A primary key indicates that a column or group of columns can be used as a unique identifier for rows in the table [...] Adding a primary key will automatically create a unique btree index on the column or group of columns used in the primary key. [...]

DrColossos
  • 12,656
  • 3
  • 46
  • 67
0

Ricola3D linked me to the same question (that's answered) that explains my problem.

Answered Question

Community
  • 1
  • 1
notbad.jpeg
  • 3,308
  • 1
  • 32
  • 37