質問

I had this problem when I try to save a new entry into a table called 'config',

class Config(models.Model):
    ident = models.CharField(max_length=uuidLength, null=True, editable=False)
    scanner = models.ForeignKey('Scanner')
    name = models.CharField(max_length=64)
   ''' some other fields '''

and postgres gave such error(the app is called "pegasus" so the table name that django gives is actually "pegasus_config"):

IntegrityError: duplicate key value violates unique constraint "pegasus_config_scanner_id_name_key"
DETAIL:  Key (scanner_id, name)=(2, ) already exists.

I searched in stackoverflow and found this solution, the problem is that I don't know which table should I reset the index. I did the following according to the answer:

SELECT setval('pegasus_config_id_seq', (SELECT MAX(id) FROM pegasus_config)+1)

but the problem still exists. I also went into database and found that "pegasus_config_scanner_id_name_key" is actually an index. So I'm confused about which index to reset? Please help. Thanks.

役に立ちましたか?

解決

You can try a query like the following to determine which table has that unique constraint defined:

SELECT  n.nspname as schema_name,
        co.conrelid::regclass as table_name,
        co.conname as constraint_name,
        pg_catalog.pg_get_constraintdef(co.oid, true) as constraing_def
FROM pg_constraint co
INNER JOIN pg_catalog.pg_class cl ON cl.oid = co.conrelid
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = cl.relnamespace
WHERE co.conname = 'pegasus_config_scanner_id_name_key'
AND co.contype = 'u'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top