Hi,

I wonder if there's a bug in the case that save() creates a new record for a 
model without data.

That's what happens if there is data provided (i.e., attributes assigned) 
(lines 158ff)

            if db_values:
                cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
                    (backend.quote_name(self._meta.db_table), 
','.join(field_names),
                    ','.join(placeholders)), db_values)
                if self._meta.has_auto_field and not pk_set:
                    setattr(self, self._meta.pk.attname, 
backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))

The last line retrieves the pk value.

Now here's the other case, a model without any attributes assigned:

            else:
                # Create a new record with defaults for everything.
                cursor.execute("INSERT INTO %s (%s) VALUES (%s)" %
                    (backend.quote_name(self._meta.db_table),
                     backend.quote_name(self._meta.pk.column),
                     backend.get_pk_default_value()))

The pk value isn't retrieved. There should be a:

                setattr(self, self._meta.pk.attname, 
backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))

Or, probaby better, move the settattr from the other case around:

            if db_values:
                cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
                    (backend.quote_name(self._meta.db_table), 
','.join(field_names),
                    ','.join(placeholders)), db_values)
            else:
                # Create a new record with defaults for everything.
                cursor.execute("INSERT INTO %s (%s) VALUES (%s)" %
                    (backend.quote_name(self._meta.db_table),
                     backend.quote_name(self._meta.pk.column),
                     backend.get_pk_default_value()))
            if not db_values or self._meta.has_auto_field and not pk_set:
                    setattr(self, self._meta.pk.attname, 
backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))


This is only from looking at the source, I'm not sure if this really bites.

Michael

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---

Reply via email to