I added a table named Payments to my Flask application, but when I tried to insert data into the new table I received the following error:
TypeError: __init__() got an unexpected keyword argument 'orderid'
when insert into the new payments table like so:
paymentdb = Payments(orderid=orderid, amount=amount, currency=currency, status="pending", wallet=response["address"], userid=current_user.id)
Even though I confirmed that all function inputs were correct, there seems to be an issue with my db/db structure. It is also strange to note that my new table is not displayed when using flask db edit. However, I can see the new table in my db file when I open it with VSCode. I also receive an error message stating that the table "users" already exists when I use flask db upgrade.
Here is the code from my models.py file:
from datetime import datetime
from flask_login import UserMixin
from src import bcrypt, db,app
class User(UserMixin, db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String, unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
created_on = db.Column(db.DateTime, nullable=False)
is_admin = db.Column(db.Boolean, nullable=False, default=False)
balance = db.Column(db.Float, nullable=False)
key = db.Column(db.String, unique=True, nullable=False)
def __init__(self, email, password, key, is_admin=False, balance=0.0):
self.email = email
self.password = bcrypt.generate_password_hash(password)
self.key = bcrypt.generate_password_hash(key)
self.created_on = datetime.now()
self.is_admin = is_admin
self.balance = 0.0
def __repr__(self):
return f"<email {self.email}>"
class Payments(db.Model):
__tablename__ = "payments"
id = db.Column(db.Integer, primary_key=True)
orderid = db.Column(db.Integer, unique=True, nullable=False)
userid = db.Column(db.Integer, nullable=False)
amount = db.Column(db.Float, nullable=False)
amount_usd = db.Column(db.Float, nullable=False)
merchant_amount = db.Column(db.Float, nullable=False)
currency= db.Column(db.String, nullable=False)
created_on = db.Column(db.DateTime, nullable=False)
status = db.Column(db.String, nullable=False)
wallet = db.Column(db.String, nullable=False)
def __init__(self, userid, amount, currency, status):
self.userid = userid
self.amount = amount
self.currency = currency
self.status = status
self.created_on = datetime.now()
self.wallet = response["address"]
def __repr__(self):
return f"<orderid {self.orderid}>"
with app.app_context():
db.create_all()
db.session.commit()
>Solution :
This doesn’t seem to be a Flask problem, but simply a mistake in how the class was constructed:
class Payments(db.Model):
...
def __init__(self, userid, amount, currency, status):
The constructor for Payments doesn’t accept an orderid argument. Perhaps you added the field but forgot to update the constructor?
class Payments(db.Model):
...
def __init__(self, orderid, userid, amount, currency, status):
self.orderid = orderid
...
I also noted another possible problem in the User constructor:
class User(UserMixin, db.Model):
...
def __init__(self, email, password, key, is_admin=False, balance=0.0):
...
self.balance = 0.0
The balance is set to 0 regardless of the value passed in the constructor.