Supabase Backend as a Service · レッスン

リレーションシップと外部キー

外部キーを使ってテーブル同士を接続し、Supabase Postgresデータベースで一対多および多対多のリレーションシップをモデル化します。

レッスン 4/413 ステップ

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

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

Why Relationships Matter

Real data is connected: a user has many posts, an order has many items. Relationships link rows across tables so you avoid duplicating data.

What Is a Foreign Key?

A foreign key is a column that points to the primary key of another table. It enforces that the referenced row actually exists.

One-to-Many Example

One author writes many books. The books table stores the author's id.

CREATE TABLE authors (
  id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  name text NOT NULL
);
CREATE TABLE books (
  id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  title text NOT NULL,
  author_id bigint REFERENCES authors(id)
);

Referential Integrity

Postgres will reject inserting a book with an author_id that does not exist. This keeps your data consistent.

ON DELETE Behavior

Decide what happens when a parent row is deleted:

  • CASCADE deletes the children too
  • SET NULL clears the reference
  • RESTRICT blocks the delete
ALTER TABLE books
  ADD CONSTRAINT fk_author
  FOREIGN KEY (author_id)
  REFERENCES authors(id)
  ON DELETE CASCADE;

Querying Across Tables

With the Supabase client you can fetch related rows in one call using nested selects.

const { data } = await supabase
  .from('authors')
  .select('name, books(title)');

Many-to-Many Relationships

When both sides can have many of the other (students and courses), use a join table holding both foreign keys.

CREATE TABLE enrollments (
  student_id bigint REFERENCES students(id),
  course_id bigint REFERENCES courses(id),
  PRIMARY KEY (student_id, course_id)
);

Composite Primary Keys

In a join table the pair of foreign keys often forms the primary key, preventing duplicate links between the same two rows.

Naming Conventions

A common convention names foreign keys as singular_id (e.g. author_id). Consistency makes nested queries predictable.

function fkName(table) {
  return table.replace(/s$/, '') + '_id';
}
console.log(fkName('authors'));

Visualizing in the Dashboard

The Supabase Table Editor shows relationship links, and the Schema Visualizer draws how your tables connect, which helps verify your design.

Putting It Together

Model connections with foreign keys, choose sensible ON DELETE rules, and use join tables for many-to-many. This is the backbone of a clean schema.

Quick Check

Test your understanding of relationships.

Recap

You learned foreign keys, one-to-many vs many-to-many relationships, ON DELETE behaviors, join tables with composite keys, and how to query related data with nested selects.

無料で開始

AI チューターと学ぶ Supabase Backend as a Service — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
11
レッスン
40

よくある質問

「リレーションシップと外部キー」レッスンは無料ですか?

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

「リレーションシップと外部キー」で何を学びますか?

外部キーを使ってテーブル同士を接続し、Supabase Postgresデータベースで一対多および多対多のリレーションシップをモデル化します。 ブラウザで直接実行するハンズオンコードでSupabase Backend as a Serviceを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Supabase Backend as a Serviceを始めるのに経験は必要ですか?

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

「リレーションシップと外部キー」レッスンにはどのくらい時間がかかりますか?

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

このSupabase Backend as a Serviceレッスンでコードを書いて実行できますか?

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

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

  1. データベーススキーマの設計
  2. テーブルとカラムの作成
  3. 基本的なデータの挿入とクエリ
  4. リレーションシップと外部キー
← Supabase Backend as a Serviceに戻る