1

The following code:

>>> from mainapp.models import ServerType
>>> server_type = ServerType()
>>> server_type.name = 'Origin'
>>> server_type.save()
>>> server_type.id
4L

generates mysql log:

SET NAMES utf8
set autocommit=0
SET SQL_AUTO_IS_NULL = 0
INSERT INTO `ServerType` (`name`, `description`) VALUES ('Origin', '')
commit

How does django know the new id without extra select statement?

django=1.4

mysql=5.1

1

1 Answer 1

2

The database adapter always responds with the new primary key on an insert statement. No extra queries required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.