title: Django: "conflicting NULL/NOT NULL declarations" from sqlite3 to Postgres
body: <p>I've been working on a sqlite3 database with a Django project. I'm moving the project to the production environment where I use Postgres. I've used South, so I have all my migrations automated.</p>
  
  <p>I'm running into the following error while trying to migrate the schema:</p>
  
  <pre><code>[05/Feb/2013 16:38:31] DEBUG [south:270] execute "ALTER TABLE "frontend_userprofile" ADD COLUMN "id" serial NULL PRIMARY KEY;" with params "[]"
  FATAL ERROR - The following SQL query failed: ALTER TABLE "frontend_userprofile" ADD COLUMN "id" serial NULL PRIMARY KEY;
  The error was: conflicting NULL/NOT NULL declarations for column "id" of table "frontend_userprofile"
  
  [..]
  
  django.db.utils.DatabaseError: conflicting NULL/NOT NULL declarations for column "id" of table "frontend_userprofile"
  </code></pre>
  
  <p>This is the relevant part of the migration file:</p>
  
  <pre><code>def forwards(self, orm):
      # Adding field 'UserProfile.id'
      db.add_column('frontend_userprofile', 'id',
                    self.gf('django.db.models.fields.AutoField')(default=None, primary_key=True, null=True),
                    keep_default=False)
  
  
      # Changing field 'UserProfile.user'
      db.alter_column('frontend_userprofile', 'user_id', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['auth.User'], unique=True, null=True))
  
  [..]
  
      'frontend.userprofile': {
          'Meta': {'object_name': 'UserProfile'},
          'company': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['frontend.Company']", 'null': 'True'}),
          'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
          'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True', 'null': 'True'})
      }
  </code></pre>
  
  <p>And of the model:</p>
  
  <pre><code>class UserProfile(models.Model):
      user    = models.OneToOneField(User, null=True, unique=True)
      company = models.ForeignKey(Company, null=True, default=None)
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>As suggested by Jordan, you can't have both a null primary key, so the following changes solve this problem:</p>
  
  <pre><code>    db.add_column('frontend_userprofile', 'id',
                    self.gf('django.db.models.fields.AutoField')(default=None, primary_key=True),
                    keep_default=False)
  
  
  class UserProfile(models.Model):
      user    = models.OneToOneField(User, unique=True)
      company = models.ForeignKey(Company, null=True, default=None)
  </code></pre>
  
  <p>But I still have a problem finishing the migration:</p>
  
  <pre><code>DEBUG [south:270] execute "ALTER TABLE "frontend_userprofile" ADD COLUMN "id" serial NOT NULL PRIMARY KEY;" with params "[]"
  FATAL ERROR - The following SQL query failed: ALTER TABLE "frontend_userprofile" ADD COLUMN "id" serial NOT NULL PRIMARY KEY;
  The error was: multiple primary keys for table "frontend_userprofile" are not allowed
  
  django.db.utils.DatabaseError: multiple primary keys for table "frontend_userprofile" are not allowed
  </code></pre>
  
  <p>I think the reason is that in a previous migration <code>user_id</code> has been set to primary key and now it's trying to add <code>id</code> as primary key. Any help with this?</p>