I'm using flask-sqlalchemy. My class is:
class Game(db.Model):
userid = db.Column(db.Integer, primary_key=True)
gameid = db.Column(db.Integer, primary_key=True)
gameid is set to auto-increment in the database.
When I run this function:
@app.route("/requestgame")
def requestgame():
game = Game()
game.userid = session["userid"]
db.session.add(game)
db.session.commit()
session["gameid"] = game.gameid
return "gameid {}".format(game.gameid)
I get "ObjectDeletedError: Instance '' has been deleted, or its row is otherwise not present."
How can I get the gameid to return?
It looks like someone asked the same question about regular SQLAlchemy here:
SQLAlchemy Obtain Primary Key With Autoincrement Before Commit
but I have tried the recommended solution (calling session.flush()
) and it doesn't seem to make any difference.