title: Django: trouble with user_id field of UserProfile after migration from MySQL to PostgreSQL
body: <p>I migrated my database from MySql to PostgreSQL, and, as I noticed that many tables weren't updated, some googling told me to do things like this:</p>
  
  <pre><code>ALTER SEQUENCE my_table_id_seq OWNED BY my_table.id;
  </code></pre>
  
  <p>and things started to work. I had to do this for all my tables.</p>
  
  <p>Then I realized that after creating a django.contrib.auth.model.User, and trying to create a UserProfile afterwards with a <code>post_save</code> hook, I would get an <code>IntegrityError</code> because the <code>user_id</code> foreign key is sent as null.</p>
  
  <p>Here's the <code>query</code> and <code>args</code> (in Python) that is sent to PostgreSQL:</p>
  
  <pre><code>INSERT INTO "auth_user" ("username", "first_name", "last_name", "email", "password", "is_staff", "is_active", "is_superuser", "last_login", "date_joined") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) (u'apitest04', '', '', u'a@a.aa', 'sha1$9f796$69c88c635c8a53a91544765d7996747cf326cfec', False, True, False, u'2013-01-05 12:39:07.384198', u'2013-01-05 12:39:07.384198')
  
  INSERT INTO "myapp_userprofile" ("user_id", "website", "job", "hobbies", "timezone", "about", "company_name", "company_description", "company_website", "retailer_country", "avatar", "default_license", "default_watermark_text", "default_watermark", "default_watermark_position", "default_watermark_opacity", "language") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) (None, None, None, None, None, None, None, None, None, None, None, 0, None, False, 0, 10, None)
  </code></pre>
  
  <p>I suppose what's happening is that the sequence id for <code>auth_user</code> is not advanced before the transaction is closed. I suspect that because the creation of the <code>User</code> is rolled back when the creation of the <code>UserProfile</code> fails, so they must be happening during the same transaction.</p>
  
  <p>Is it possible that the <code>id</code> of the <code>User</code> is somehow not returned yet, because the transaction has not ended yet? And how do you propose I fix the problem?</p>