I have a function that the user can add a budget. In the python shell, when I am adding a budget there is no error. But when I am going for the route where I am adding my budget, it gets an error.
This is the error I get:
sqlalchemy.exc.IntegrityError
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: budgets.new_budget
[SQL: INSERT INTO budgets (new_budget, date_posted, budget_id) VALUES (?, ?, ?)]
[parameters: (None, '2021-03-11 10:11:13.725417', 1)]
(Background on this error at: http:
The error specifies in db.session.commit().
routes.py:
@app.route("/add_budgets", methods=['GET', 'POST'])
def add_budgets():
form = BudgetForm()
if form.validate_on_submit:
user_budget = Budgets(new_budget=form.budget.data, budget_id=current_user.id)
db.session.add(user_budget)
db.session.commit()
return redirect(url_for('add_budgets'))
posted_budget = Budgets.query.all()
posted_budget = current_user.current_budget
return render_template("add_budget.html", posted_budget=posted_budget)
models.py:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(db.String(20), nullable=False)
current_budget = db.relationship('Budgets', backref='user_budget', lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.email}')"
class Budgets(db.Model):
id = db.Column(db.Integer, primary_key=True)
new_budget = db.Column(db.Integer, nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
budget_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
expense = db.relationship('Expenses', backref='budget_expenses', lazy=True)
def __repr__(self):
return f"Budgets('{self.new_budget}')"
What I have tried:
I tried to use
db.drop_all()
and create again the database by using
db.create_all()
, nothing happens