Datensätze erstellen und Sessions committen
Fügen Sie Objekte hinzu und speichern Sie sie mit der Session.
Datensätze erstellen und Sessions committen ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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), 201Roll 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! 🎉
Häufig gestellte Fragen
Ist die Lektion „Datensätze erstellen und Sessions committen“ kostenlos?
Ja — der vollständige Text von „Datensätze erstellen und Sessions committen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Datensätze erstellen und Sessions committen“?
Fügen Sie Objekte hinzu und speichern Sie sie mit der Session. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Flask Academy zu starten?
Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Datensätze erstellen und Sessions committen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?
Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Datensätze erstellen und Sessions committen
- Endpoints für einzelne und mehrere Datensätze
- Bestehende Datensätze sicher aktualisieren
- Löschen und fehlende Zeilen behandeln