SQLAlchemy throws an IntegrityError due to DBSession.add ()

The second line of time 121 is called in this script http://paste.pocoo.org/show/520040/ , I get this error message:

*** IntegrityError: (IntegrityError) duplicate key value violates unique constraint "heroes_pkey" DETAIL: Key (id)=(14) already exists. 'INSERT INTO heroes (id, name, description, image_name, default_filename, faction, stat, roles, strength, agility, intelligence, strength_gain, agility_gain, intelligence_gain, min_hp, max_hp, min_mana, max_mana, min_damage, max_damage, armor, movespeed, attack_range, min_attack_animation, max_attack_animation, min_cast_animation, max_cast_animation, base_attack_time, missile_speed, day_site_range, night_site_range, resource_name, "order") VALUES (%(id)s, %(name)s, %(description)s, %(image_name)s, %(default_filename)s, %(faction)s, %(stat)s, %(roles)s, %(strength)s, %(agility)s, %(intelligence)s, %(strength_gain)s, %(agility_gain)s, %(intelligence_gain)s, %(min_hp)s, %(max_hp)s, %(min_mana)s, %(max_mana)s, %(min_damage)s, %(max_damage)s, %(armor)s, %(movespeed)s, %(attack_range)s, %(min_attack_animation)s, %(max_attack_animation)s, %(min_cast_animation)s, %(max_cast_animation)s, %(base_attack_time)s, %(missile_speed)s, %(day_site_range)s, %(night_site_range)s, %(resource_name)s, %(order)s)' {'day_site_range': 1800, 'min_damage': 47, 'intelligence': 18.0, 'agility': 14.0, 'night_site_range': 800, 'min_attack_animation': 0.4, 'id': 66666, 'attack_range': 128, 'default_filename': None, 'strength_gain': 2.7, 'strength': 21.0, 'min_hp': 549, 'armor': 1.96, 'intelligence_gain': 1.5, 'movespeed': 300, 'max_hp': 1765, 'max_cast_animation': 0.51, 'stat': 'Strength', 'resource_name': None, 'description': None, 'faction': 'Radiant', 'missile_speed': 0, 'image_name': None, 'max_damage': None, 'min_cast_animation': 0.4, 'max_mana': 702, 'name': None, 'roles': 'Carry / Pusher', 'base_attack_time': 1.7, 'agility_gain': 1.3, 'min_mana': 234, 'max_attack_animation': 0.3, 'order': 4} 

I understand that the reason I most likely get this error is b / c the previous session.add () is trying to add a record to the database with a primary key that already exists. The part that I don’t understand is usually this happens automatically when using Postgresql, so why this situation is unique, that automatic increment does not happen automatically when adding (). It is also important to note that the number in this part of the error message "(id) = (14)" increases by one with each successful call to the script.

+6
source share
1 answer

The problem ended up with Postgres. I created the database by importing the sql file, and the sequence that tracked the primary key of the heroes turned out to be inaccurate. This explains why the identifier increased by one at each subsequent start, b / c Postgres tried to find an unused primary key. I solved this by issuing the following query in my database.

 SELECT setval('heroes_id_seq', MAX(id)) FROM heroes; 
+12
source

Source: https://habr.com/ru/post/903643/


All Articles