Search code examples
pythonflaskpeeweeflask-peewee

Peewee creates mutiple records (on flask app)


I have a simple statement,

Points.create(user=bt.user,match=m,value=2,datecreated=date.today())

I expect it to create one record but sometimes it creates mutliple records. Not sure what's going on.

Complete code is like below:

from utils.models import *
import sys
m = Match.select().where(Match.id == sys.argv[1]).get()
r = Result.select().where(Result.match == m).get()
w= r.winner
print(m.id,m.team1.name,m.team2.name,m.mdate,m.mtime)
if Bet.select().where(Bet.match == m).exists():
           for bt in Bet.select().where(Bet.match == m):
               print("User:::"+str(bt.user.name))
               if w.id == bt.bet.id:
                   print("+2")
                   Points.create(user=bt.user,match=m,value=2,datecreated=date.today())
               else:
                   print("-2")
                   Points.create(user=bt.user,match=m,value=-2,datecreated=date.today())


This is the end result:
(u'Tony George', 2)
(u'AJ', 2)
(u'Tony George', 2)
(u'AJ', 2)
(u'Aravind S', 2)
(u'Iyyam', 2)
(u'Rajakumar', 2)
(u'Leo', 2)

As you can see it seems to create 2 records for Tony George. This happens arbitrarily, sometimes for one user and sometimes for another.


Solution

  • Check out:

    Short answer is you should consume the first query:

    for bt in list(Bet.select().where(Bet.match == m)):