Django对象创建和Postgres序列 [英] Django object creation and Postgres Sequences

查看:141
本文介绍了Django对象创建和Postgres序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导入脚本,它运行一系列命令,从一个Postgres数据库到另一个,都运行相同的Django代码库。在大多数情况下,它使用./manage.py loaddata进行复制,但有些对象需要额外的处理,并且在自定义脚本中使用Django的objects.create()方法来复制数据。当这样做时,我指定ID,即

  MyObject.objects.create(id = 2,title ='foo' )

一旦脚本完成,我注意到表格上的Postgres SEQUENCE是错误的我做了objects.create()。即,进口之前是50,还有50以后,即使表现在有150个对象。这当然会导致创建新对象时出错,因为它尝试使用已经存在的ID(在所有这些表上,ID只是一个香草自动增量字段)。但是,通过./manage.py loaddata填充的表似乎很好。



我知道我可以使用Django的./manage.py手动重置这些表sqlsequenreset,但我很好奇为什么序列似乎摆脱了首先。 objects.create()不会增加吗?我可以忽略某些明显的东西吗?

解决方案

一切正常。 django的create()直接与序列引入无关。简单地说:




  • postgresql自动递增
    ('serial'type)只是
    '的创建序列+创建整数
    字段,序列值为默认值

  • django的autofield主键(id
    如果没有指定,则为整数)
    只创建一个序列号字段

  • 当您手动指定ID时,
    postgres将值插入到
    数据库中。当你指定一个值时,
    它省略了'default'参数,
    这是一个正确的行为。





另一个想法是您创建该对象然后更新该ID(当然它不能被使用),并且最后将序列值设置为最大值。


I have an import script which runs a series of commands to get things from one Postgres DB to another, both running the same Django codebase. For the most part, it uses ./manage.py loaddata to copy over, but some objects require additional processing and I use Django's objects.create() method in a custom script to copy the data. When doing this, I specify the ID, i.e,

MyObject.objects.create(id = 2, title = 'foo')

Once the script is done, I'm noticing that the Postgres SEQUENCE is wrong on the tables where I did objects.create(). I.e., it was 50 before the import, and still 50 after, even though the table now has 150 objects. This, of course, leads to errors when creating new objects, since it tries to use an ID which already exists (On all these tables, the ID is just a vanilla auto-increment field). However, the tables which were filled via ./manage.py loaddata seem fine.

I'm aware that I can manually reset these tables with Django's ./manage.py sqlsequenreset, but I'm curious as to why the sequence seems to get out of whack in the first place. Does objects.create() not increment it? Am I overlooking something obvious?

解决方案

everything works fine. django's create() has nothing to do with sequence incementation directly. briefly:

  • postgresql auto incrementing ('serial' type) is just a shortcut of 'create sequence + create integer field with sequence value as default'
  • django's autofield primary key (id integer if not specified else by you) just creates a serial field
  • when you specify the id manually, postgres inserts the value into the database. when you specify a value, it omits the 'default' parameter, which is a proper behavior.

so, if you want your inserts to increment the sequence in a way of your choice, you need to manually change the sequence value using SELECT setval('sequence_name', int_value); otherwise leave it null and it will increment automatically - select the current val and increment it +1 (if not specified differently in the sequence definition).

another idea is you create the object and then update the id (of course it can't be already used) and in the end set the sequence value to the max id.

这篇关于Django对象创建和Postgres序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆