Django ORM误读PostgreSQL序列? [英] Django ORM misreading PostgreSQL sequences?

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

问题描述

背景:运行Django应用程序的PostgreSQL数据库(Django 1.1.1,Python2.4,psycopg2和Postgres 8.1)我已经从SQL转储数据库恢复了好几次。每次我这样做,然后尝试添加一个新的行,shell,管理员或站点前端,我得到这个错误:



IntegrityError:重复键违反唯一约束app_model_pkey



数据转储很好,正在重置序列。 但是,如果我再次尝试添加该行,那就成功了!所以我可以尝试在每个表格中插入一个新行,然后一切似乎都是copacetic。



问题:鉴于(1)SQL转储很好,Postgres正确读取它(每个早期问题)和(2 )Django的ORM似乎没有系统地获得下一个值,在这个具体实例中发生了什么?

解决方案

Django不以任何方式持有或直接阅读序列值。我已经解释了f.ex.在此问题:2088210 / django-object-creation-and-postgres-sequences



当您尝试添加行时,Postgresql会增加序列,即使操作的结果不成功(引发重复键错误)序列增量也不会 t回滚。所以,这就是为什么第二次尝试添加行的原因。



我不知道为什么你的序列设置不正确,你可以检查在转储和还原之前的序列值是多少,并且对max )pk的表?也许这是恢复的8.1错误?我不知道。我确信:这不是Django的错。


Background: Running a PostgreSQL database for a Django app (Django 1.1.1, Python2.4, psycopg2 and Postgres 8.1) I've restored the database from a SQL dump several times. Each time I do that and then try to add a new row, either shell, admin, or site front end, I get this error:

IntegrityError: duplicate key violates unique constraint "app_model_pkey"

The data dump is fine and is resetting the sequences. But if I try adding the row again, it's successful! So I can just try jamming a new row into every table and then everything seems to be copacetic.

Question: Given that (1) the SQL dump is good and Postgres is reading it in correctly (per earlier question), and (2) Django's ORM does not seem to be failing systemically getting next values, what is going on in this specific instance?

解决方案

Django doesn't hold or directly read the sequence values in any way. I've explained it f.ex. in this question: 2088210/django-object-creation-and-postgres-sequences.

Postgresql does increment the sequence when you try to add a row, even if the result of the operation is not successful (raises a duplicate key error) the sequence incrementation doesn't rollback. So, that's the reason why it works the second time you try adding a row.

I don't know why your sequences are not set properly, could you check what is the sequence value before dump and after restore, and do the same with the max() pk of the table? Maybe it's an 8.1 bug with the restore? I don't know. What I'm sure of: it's not Django's fault.

这篇关于Django ORM误读PostgreSQL序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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