DETAIL: Key (name)=(gradenumber) already exists

20 views
Skip to first unread message

li yun

unread,
May 6, 2021, 8:42:02 AM5/6/21
to peewee-orm
I migrated from mysql to postgresql, and it is very strange why the following code will make an error. Is there something wrong with me?

class Currnumber(Model):
          name = CharField(unique=True)
          number = IntegerField(null=True)
          class Meta:
                    database = db

try:
     Currnumber.create(name="gradenumber", number=self.bnumber)
except:
      Currnumber.update(number=self.bnumber).where(Currnumber.name == "gradenumber").execute()  

li yun

unread,
May 6, 2021, 9:13:50 AM5/6/21
to peewee-orm
In addition, does peewee not support Postgresql transactions? I smoothly migrated the code from mysql to Postgresql, and the code of the following transactions can not be executed normally. I commented the commit, but it can still be written to the database normally. Should I not be able to write to the database without commit?

with db.manual_commit() as tx:
        db.begin()
        for b in range(curr_storage_hight, curr_storage_hight + 1000):
              if b > end_block:
                       print("current wait new block")
                       sleep(20)
                       break
       self.starttime = datetime.datetime.now()
       self.bnumber = b
       c =Currnumber.get_or_none(name="gradenumber")
       if c ==None:
          Currnumber.create(name="gradenumber", number=self.bnumber)
       else:
          Currnumber.update(number=self.bnumber).where(Currnumber.name == "gradenumber").execute()
# db.commit() 
Reply all
Reply to author
Forward
0 new messages