views:

40

answers:

2

Hi,

I have a strange behaviour on my Django/PostgreSQL system.

After saving a model object the primary key is none although it's an AutoField and the id is correctly saved in the database.

The following script passage returns None for the id:

a = SomModelClass()
a.someattribute = 'xyz'
a.save()
a.someattribute
>>> 'xyz'
a.id
>>> None

The model class looks somehow like this:

class SomeModelClass(models.Model):
    id = models.AutoField(db_column = 'id', primary_key = True)
    someattribute = models.CharField(db_column = 'someattribute', max_length = 200)

This behaviour occurs only on this model; all other models work fine.

The problem appeared one day without changing the model structure.

Perhaps there is some problem with the data integrity of the database? Using another database server it works fine.

Best regards!

+1  A: 

Can you check you Postgresql log and find out what query is being fired? This might give some clues. Also write a quick unit test to exercise the same code and see if it works.

Manoj Govindan
Hi,Thanks for the answer! I checked the queries and isolated the problem. The following query returns no value: SELECT CURRVAL(pg_get_serial_sequence('<<the correct table name>>','id'))
o.elias
A: 

Hi,

I solved the problem now. The relationship between the sequence and the serial column was somehow destroyed. A simple

ALTER SEQUENCE <<sequence_name>> OWNED_BY <<table_name>>.<<pk_column_name>> 

solved the problem.

Best regards!

o.elias