2

I created my models and everything was running smooth, but I had to create another field, so I created it and added it in the model definition. After that (I'm not 100% it was that the cause) Django's admin tries to ADD the NULL value in the ID field.

Initial model:

class Image(models.Model):
    setid = models.ForeignKey(Imageset, null=False)
    originalwidth = models.PositiveIntegerField(editable=False)
    originalheight = models.PositiveIntegerField(editable=False)
    originalimage = models.ImageField('Imagem', upload_to=image_upload_to, height_field='originalheight', width_field='originalwidth')
    alt = models.CharField('Alt', max_length=128, help_text='ALT da imagem', null=False, blank=False)
(removed from brevity...)

    def save(self):
        super(Image, self).save()
        self.create_thumbnail()

Then I added this field

thumbnail = models.ImageField(max_length=128, null=True, editable=False, upload_to=True)

And added manually the thumbnail field in the Database.

Admin is working really fine for UPDATES, but It's refusing to ADD new records. The error is "Violation of non-null constraint". DETAIL: Failing row contains (null, 1, 700, 701, f/3/f3d4357f7c697aa08dc65f7d866cc30d_14.jpg, ddd, , ).

The method create_thumbnail is fine, i just removed it, removed the custom save and nothing seems to work.

According to that message, Django is trying to run something like this:

INSERT INTO "public"."imageset_image" ("id","setid_id","originalwidth","originalheight","originalimage","alt")
VALUES (null, 1, 999, 999, 'a/4/a49ba4fc8cbc32f760cb51d84cf9d0cc.jpg', 'alt')

wich is kinda silly and produces the same error. But, when I try to do the right SQL

INSERT INTO "public"."imageset_image" ("setid_id","originalwidth","originalheight","originalimage","alt")
VALUES (1, 999, 999, 'a/4/a49ba4fc8cbc32f760cb51d84cf9d0cc.jpg', 'alt')

ERROR Again.

Seems that for some reason Postgresql forgot the SERIAL even though they are there

SELECT * FROM information_schema.sequences WHERE sequence_catalog='catalogname' AND sequence_schema='public' AND sequence_name='imageset_image_id_seq'

Any ideas? It was working fine, I added several records..... thanks in advance

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • I don't use django... but as a guess; maybe your `id` column is not set as a primary key, so your ORM is treating it just like a normal col?.. use `psql` to check the table definition. – Chris Farmiloe Apr 09 '13 at 15:50
  • It really seems like its the problem, but all PK rules are there. Maybe some RULE--TABLE is broken. Besides, when I run `manage.py inspectdb` django detects all ID fields are indeed `id = models.IntegerField(primary_key=True)` – Alexei Martchenko Apr 09 '13 at 16:31
  • LOL I discovered the answer but I can't post it here because I don't have enough reputation. Yes, the PK was there, the ID was there but (probably) Django deleted the default value needed in order to make PKs work as serial (autoincremented) fields. Wich is `ALTER TABLE imageset_image ALTER COLUMN id SET DEFAULT nextval('imageset_image_id_seq'::regclass);` – Alexei Martchenko Apr 09 '13 at 17:18

2 Answers2

1

After few more tests and diggin' into my primary suspect: Postgresql, i've found the solution:

ALTER TABLE imageset_image ALTER COLUMN id SET DEFAULT nextval('imageset_image_id_seq'::regclass);

The question now is: Since it was previously working and i've been using the admin. WHY THIS HAPPEN? Maybe Django's syncdb after adding a new field is ERASING the default value of the id field? This wasn't supposed to happen.

0

Did you migrate your database after adding the model field? I'd recommend that you take a look at using South for migrations in the future. It's great. This question covers the topic nicely.

Aside from that, in the second bit of code it looks like you're inserting setid_id but not the id itself. If you still want to go the manual route, try:

INSERT INTO "public"."imageset_image" ("id", "setid_id","originalwidth","originalheight","originalimage","alt")
VALUES (1, 1, 999, 999, 'a/4/a49ba4fc8cbc32f760cb51d84cf9d0cc.jpg', 'alt')
Community
  • 1
  • 1
Kevin London
  • 4,628
  • 1
  • 21
  • 29