Trên lần thứ hai dòng 121 được gọi là trong kịch bản này http://paste.pocoo.org/show/520040/, tôi nhận được thông báo lỗi này:SQLAlchemy được Ném một IntegrityError do DBSession.add()
*** 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}
Tôi hiểu rằng lý do tôi nhất có khả năng nhận được lỗi này, là b/c session.add trước đó() đang cố gắng thêm bản ghi vào cơ sở dữ liệu với khóa chính đã tồn tại. Phần tôi không hiểu, thường điều này xảy ra tự động trong khi sử dụng Postgresql, vậy tại sao tình huống này là duy nhất mà tự động gia tăng không xảy ra tự động trên add(). Nó cũng quan trọng đối với ghi chú, rằng số trong phần này của thông báo lỗi "(id) = (14)" tăng thêm một số với mỗi cuộc gọi thành công của tập lệnh.