I'm having an issue.
I'm creating a School task manager for myself. I want that when i delete a schoolwork, all the tasks associate with this schoolwork are deleted too, and it does not work.
Here is the code:
this is the class for the schoolwork tasks and Schoolwork:
class Sw_task(db.Model):
id = db.Column(db.Integer, primary_key=True)
task = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime(), default=datetime.now())
duedate = db.Column(db.DateTime())
details = db.Column(db.String(200))
user = db.Column(db.String())
sw = db.Column(db.Integer(), default=0)
course = db.Column(db.Integer())
sw_id = db.Column(db.Integer, db.ForeignKey('schoolwork.id', ondelete="CASCADE"))
class Schoolwork(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(), nullable=False)
value = db.Column(db.Integer())
team = db.Column(db.Integer(), default = 0 )
time = db.Column(db.Integer())
part = db.Column(db.String(500))
user = db.Column(db.String())
duedate = db.Column(db.DateTime())
course = db.Column(db.Integer(), default=0)
other_infos = db.Column(db.String())
tasks = db.relationship("Sw_task", backref="schoolwork", passive_deletes=True, cascade='all, delete', lazy=True)
Here is the code for creating a task:
@app.route("/sw/<int:id>", methods=["GET", "POST"])
@login_required
def schoolwork_ind(id):
global now
schoolwork_current = Schoolwork.query.get(id)
sw_id = schoolwork_current.id
sw_tasks = Sw_task.query.all()
course_id = id
due_date = schoolwork_current.duedate.date()
due_in = (due_date - now).days
from_course = Course.query.get(id)
course_id = from_course.id
user = current_user.email
print(course_id)
if request.method=="POST" and "taskname" in request.form:
print(user)
task=request.form["taskname"]
dated = request.form['duedate']
date_goodsize = dated.replace("-", "/")
sw_course = from_course
due = datetime.strptime(date_goodsize,"%Y/%m/%d" )
details = request.form['details']
new_task = Sw_task(sw_id=sw_id, course=course_id,task=task, duedate=due, user=user, sw=sw_id, details=details)
db.session.add(new_task)
db.session.commit()
redirect_link = "/sw/" str(sw_id)
return redirect(redirect_link)
return render_template("schoolwork.html", sw = schoolwork_current, course=from_course, swtasks=sw_tasks, now=now, duein=due_in)
And here is the code to delete a schoolwork:
@app.route('/delete/sw/<int:id>', methods=["GET", "POST"])
def delete_sw(id):
to_delete = Schoolwork.query.get_or_404(id)
redirect_var = "/schoolworks"
try:
db.session.delete(to_delete)
db.session.commit()
except:
return"There has been a problem. Please try again"
return redirect(redirect_var)
I really hope you can help me...
I know that there is a similar post in this forum, but even with this it doesnt work.
CodePudding user response:
Try modifying your Sw_task.sw_id
column to this:
sw_id = db.Column(db.Integer, db.ForeignKey('schoolwork.id'))
Also, change your Schoolwork.tasks
column to this:
tasks = db.relationship("Sw_task", backref="schoolwork", cascade='all, delete', lazy=True)
I have a very similar project to this, and the implementation above for deleting an object after deleting its parent, works for me.