tickets: 8799
This data as json
id | created | changetime | last_pulled_from_trac | stage | status | component | type | severity | version | resolution | summary | description | owner | reporter | keywords | easy | has_patch | needs_better_patch | needs_tests | needs_docs | ui_ux |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
8799 | 2008-09-02 15:45:19 | 2008-09-02 16:59:57 | 2022-03-06 03:43:12.046010 | Unreviewed | closed | Database layer (models, ORM) | invalid | .save() and .objects.create() does not set the primary key when the model has an explicit primary key field | Here is what I observed on django1.0-beta2 and 0.96 releases with MySQL or Sqlite3.[[BR]] I have the following table:[[BR]] Sqlite: {{{ CREATE TABLE "Person" ( "id" integer NOT NULL PRIMARY KEY, "name" text NOT NULL ) MySQL: CREATE TABLE `Person` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` longtext NOT NULL ) }}} In the models.py I have {{{ class Person(models.Model): id = models.IntegerField(primary_key=True) name = models.TextField() class Meta: db_table = u'Person' }}} When I issue: {{{ p = Person.objects.create(name='Bob') p.id }}} I get nothing. The record however is persisted. Same happens with {{{ p = Person('name'=Bob) p.save() p.id }}} If however the model is missing the id field and django auto generates it: {{{ class Person(models.Model): name = models.TextField() class Meta: db_table = u'Person' }}} Than everything is OK. The id is set correctly. | nobody | vmihaylov@gmail.com | 0 | 0 | 0 | 0 | 0 | 0 |