Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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://sqlalche.me/e/13/gkpj)


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
Posted
Updated 13-Oct-22 13:45pm
v2
Comments
Richard MacCutchan 11-Mar-21 9:17am    
The message is clear, a parameter is missing from the insert operation.
meister meister 12-Mar-21 2:49am    
I have existing data in budgets before that happen(sorry I didn't include it in my question). But now I found the problem which is the template inheritance of that route.
NotTodayYo 12-Mar-21 11:39am    
The error seems to indicate you are trying to put NULL into a column that does not support NULLs.
Member 15797123 13-Oct-22 19:46pm    
Were you able to solve this? Having the same issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900