0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Error: Failed to run sql query: ERROR: 42P07: relation "events" already exists

0
Posted at

概要

Supabaseでテーブルを作成するときに以下のエラーが発生した!

Error: Failed to run sql query: ERROR: 42P07: relation "events" already exists

エラー:SQLクエリの実行に失敗しました:ERROR: 42P07: テーブル「events」はすでに存在します

eventsというテーブルは先ほど作成したのですが、エラーログが出たので、やり直そうとSQLを再度実行。しかしエラーが発生!

すでにテーブルが作成されているので、削除しないと新しく定義したSQLは実行できません。

Google Chrome 2026-04-10 07.28.02.png

解決策

作成したテーブルを一度削除して再度テーブルを作成します。

drop table if exists public.events;

実行するSQL

-- 既存のテーブルを一度削除(これでエラーを回避)
drop table if exists public.events;

-- 改めて最新のテーブル作成
create table public.events (
  id uuid not null default gen_random_uuid() primary key,
  user_id uuid not null references auth.users(id) on delete cascade default auth.uid(),
  title text not null,
  description text,
  event_date date not null,
  start_time time without time zone,
  is_done boolean default false,
  created_at timestamptz default now(),
  updated_at timestamptz default now()
);

-- 複合インデックス
create index events_user_id_event_date_idx on public.events (user_id, event_date);

-- RLS設定
alter table public.events enable row level security;

-- ポリシー設定
create policy "Users can only access their own events"
  on public.events for all
  using (auth.uid() = user_id)
  with check (auth.uid() = user_id);

実行結果

実行して良いかモーダルが表示されるので、Run this queryを実行します。

CleanShot 2026-04-10 at 07.31.31.png

実行するとテーブルが削除され新しく同じ名前のテーブルが作成されます。

CleanShot 2026-04-10 at 07.31.42.png

最後に

もしSupabaseでエラーが発生した場合は、Dashboardに備えてあるAIアシスタンを使用するか、ChatGPT、Geminiを活用してみましょう。

今回でたエラーなのですが、海外のサイトしかトップに表示されなかったので、記事にしようと思いました。過去に何度も遭遇したことがあるのですが、久しぶりにSQL触ると忘れてしまいますね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?