0Pricing
Flask Academy · Lesson

Update Existing Records Safely

Mutate fields and flush changes correctly.

Update Existing Records Safely is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The U in CRUD

Updating means changing an existing row, not making a new one. The trick is to load the record first, then edit it. ✏️

Find Before You Change

Always fetch the target row by its id before touching it. get_or_404() loads it or stops with a clean 404.

user = User.query.get_or_404(user_id)

Edit Like a Python Object

Once loaded, just reassign its attributes. SQLAlchemy notices the change and marks the object as dirty for you.

user.name = "New Name"

Read the New Values

Updates usually carry the new data in the request body. Parse it with get_json() before applying anything.

data = request.get_json()

Apply Fields Carefully

Only overwrite fields the client actually sent. Using get() with a fallback avoids wiping values to None by accident.

user.name = data.get("name", user.name)

Commit the Change

No add() is needed for an existing row. A single commit() flushes your edits to the database.

db.session.commit()

PUT Replaces, PATCH Tweaks

Use PUT when the client sends the whole object and PATCH when it sends only the fields it wants to change.

@app.route("/users/<int:id>", methods=["PATCH"])

Validate Before Saving

Check incoming values before you commit. Reject bad data with a 400 so you never persist a broken record.

if not data.get("name"):
    return jsonify(error="name required"), 400

Roll Back if It Breaks

If the commit raises, call rollback() to discard the half-applied edit and leave the row exactly as it was.

except Exception:
    db.session.rollback()

Return the Updated Row

A good update reply sends back the fresh record with status 200, so the client sees the saved result.

return jsonify({"id": user.id, "name": user.name}), 200

Load, Edit, Commit

The whole update flow is three beats: load the row, change its attributes, then commit. No new object ever appears.

Quick Check

You changed a loaded object's attribute. What must happen for the edit to stick?

Recap: Updating Records

You load with get_or_404, apply only sent fields, validate, then commit, rolling back on errors. Safe updates unlocked! 🎉

Frequently asked questions

Is the “Update Existing Records Safely” lesson free?

Yes — the full text of “Update Existing Records Safely” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Update Existing Records Safely”?

Mutate fields and flush changes correctly. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Update Existing Records Safely” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Create Records and Commit Sessions
  2. Read One and Read Many Endpoints
  3. Update Existing Records Safely
  4. Delete and Handle Missing Rows
← Back to Flask Academy