开发者

Django: Difference between save() and create() from transaction perspective

The create() method in Django creates a model instance then calls save(), which is said to trigger commit. So there should not be any difference in triggering transaction's commit.

But in reality, executing a method that creates a bunch of model instances using create() on Postgresql I am getting transaction aborted, commands ignored until end of transaction exception. The method runs fine with non-transactional db backends. Also, when I replace the create()s with:

m = Model(attr1=..., attr2=...)
m.save()

it runs on Postgresql fine too.

Is there a difference between using 开发者_C百科save() and create() in the sense of transactions?

edit: create() also sets self._for_write = True before calling save(), but I couldn't trace it to see if it has any effect on transaction behavior.

edit: example code can be found here.


As you've probably seen, create() is just a wrapper for save():

The _for_write part is most probably meant only for database selection, so I wouldn't pay too much attention to it.

And regarding that "transaction aborted" error, without seeing your code it's hard to say what the problem is. Maybe you e.g. violated UNIQUE constraint with create(), which causes PostgreSQL to demand transaction rollback, and then you tried save() with different data - it's hard to tell without the exact code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜