0Pricing
Flask Academy · Lesson

Create Records and Commit Sessions

Add objects and persist with the session.

Create Records and Commit Sessions is a free Flask Academy lesson on CoddyKit — lesson 1 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 C in CRUD

Creating a record means turning incoming data into a new row in your table. In Flask-SQLAlchemy that flow runs through the session. ✨

Build the Object First

Start by making a fresh model instance with the values you want. Nothing has touched the database yet, it is just a Python object.

user = User(name="Ada", email="ada@mail.com")

Add It to the Session

Tell SQLAlchemy to track the new object by calling db.session.add(). This stages it, but it is still not saved.

db.session.add(user)

Commit to Save

Nothing is permanent until you call db.session.commit(). That flushes your changes and writes the row to the database.

db.session.commit()

The Id Appears After Commit

Once you commit, the database assigns a primary key. Read it back from user.id to confirm the row really exists.

db.session.commit()
print(user.id)

A Full Create Endpoint

Inside a POST view you read the body, build the object, add it, and commit. That is the whole create path in four lines.

data = request.get_json()
user = User(name=data["name"])
db.session.add(user)
db.session.commit()

Return 201 Created

A successful create should answer with status 201, the code that means a new resource was made on the server.

return jsonify(id=user.id), 201

Roll Back on Failure

If a commit fails, wrap it in try and call db.session.rollback() to undo the staged change and keep your session clean.

try:
    db.session.commit()
except Exception:
    db.session.rollback()

Add Many at Once

Need several rows? Stage them all with add_all(), then a single commit saves the whole batch together.

db.session.add_all([u1, u2, u3])
db.session.commit()

One Commit, One Transaction

Every add since the last commit is part of one transaction. They all succeed together, or none of them land if it fails.

Add Stages, Commit Saves

Remember the rhythm: build the object, add to stage it, then commit to save. Skipping commit means your data never persists.

Quick Check

You called add() but the row is missing from the table. What did you forget?

Recap: Creating Records

You build an object, add it, and commit to save, returning 201 and rolling back on errors. That is a solid create endpoint! 🎉

Frequently asked questions

Is the “Create Records and Commit Sessions” lesson free?

Yes — the full text of “Create Records and Commit Sessions” 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 “Create Records and Commit Sessions”?

Add objects and persist with the session. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Create Records and Commit Sessions” 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