0Pricing
Flask Academy · レッスン

レコードを作成してセッションをコミットする

オブジェクトを追加し、セッションで永続化します

「レコードを作成してセッションをコミットする」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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! 🎉

よくある質問

「レコードを作成してセッションをコミットする」レッスンは無料ですか?

はい。「レコードを作成してセッションをコミットする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「レコードを作成してセッションをコミットする」で何を学びますか?

オブジェクトを追加し、セッションで永続化します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「レコードを作成してセッションをコミットする」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. レコードを作成してセッションをコミットする
  2. 単一取得と複数取得のエンドポイント
  3. 既存レコードを安全に更新する
  4. 削除と存在しない行の処理
← Flask Academyに戻る