0

So, in a bit more detail I have a model with a field like: permalink = models.IntegerField(default=0)

I've not actually been using this field - but would now like to.

However, it seems all models on this table, permalink is now 57295730 - on all 2000 models!

In an attempt to debug, I tried completly wiping the DB, running migrate (~100 migrations) - but then creating a instance of the model, I am told permalink violates the not-null constraint though I am definitely passing it a value! I also get a list of the values I am passing it, but am not sure how to know which value/column relates to which field?

I've even tried removing DB, removing migrations, running a new makemigrations - and still get the null violation...

even stranger, it looks like this field has not been touched since the initial migration!

migrations$ egrep permalink * 0001_initial.py: ('permalink', models.IntegerField(default=0)), migrations$

I'm running (k)ubuntu 14.04, postgres 9.3, python 3.4, django 1.9.4

Though I'd love to know how to fix this - my question is really "What can I do to debug this kind of situation?"

Chozabu
  • 1,015
  • 1
  • 10
  • 33
  • Also - cannot edit the value of `permalink` admin says it has changed, but it remains unchanged. All other data _looks_ OK and _seems_ to edit OK too. – Chozabu Mar 30 '16 at 00:22

1 Answers1

0

Well Not the answer I want - but a working answer:

  1. do automated testing
  2. use CI! Prevent this problem in the first place

And if you are not doing the above... use git bisect (or if you cant, manually use git reset --hard HEAD~1 to find the problem!)

in mycase, I was over-riding the save function of the model in a... stupid way!

edit: in a little more detail, I was setting permalink to be 1 greater than the current biggest value - but in an earlier commit, had removed the + 1

However, I did not notice this error quickly, as it did not happen with data in the DB.

So! the error was actually quite informative - had I been running my tests more often (or using CI) I would have been informed of the error instantly, and saved myself quite a headache!

so, in short: **write tests, run them - automatically **

Chozabu
  • 1,015
  • 1
  • 10
  • 33