You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lesson: App Router
Chapter: 6
Database provider: Supabase (It is likely the error will occur with other providers as well)
Seeding the database with /seed fails and returns the following response with 500 status.
Possible cause
From what I understand the seed functions do not make use of the scoped sql instance provided by sql.begin and instead end up using the sql instance declared at the top of /seed/route.tsx file.
Fix
Update /seed/route.tsx to make use of the scoped sql instance supplied by sql.begin
importbcryptfrom'bcrypt';importpostgresfrom'postgres';import{invoices,customers,revenue,users}from'../lib/placeholder-data';constsql=postgres(process.env.POSTGRES_URL!,{ssl: 'require'});asyncfunctionseedUsers(sql: postgres.TransactionSql){awaitsql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;awaitsql` CREATE TABLE IF NOT EXISTS users ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(255) NOT NULL, email TEXT NOT NULL UNIQUE, password TEXT NOT NULL ); `;constinsertedUsers=awaitPromise.all(users.map(async(user)=>{consthashedPassword=awaitbcrypt.hash(user.password,10);returnsql` INSERT INTO users (id, name, email, password) VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword}) ON CONFLICT (id) DO NOTHING; `;}),);returninsertedUsers;}asyncfunctionseedInvoices(sql: postgres.TransactionSql){awaitsql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;awaitsql` CREATE TABLE IF NOT EXISTS invoices ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, customer_id UUID NOT NULL, amount INT NOT NULL, status VARCHAR(255) NOT NULL, date DATE NOT NULL ); `;constinsertedInvoices=awaitPromise.all(invoices.map((invoice)=>sql` INSERT INTO invoices (customer_id, amount, status, date) VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date}) ON CONFLICT (id) DO NOTHING; `,),);returninsertedInvoices;}asyncfunctionseedCustomers(sql: postgres.TransactionSql){awaitsql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;awaitsql` CREATE TABLE IF NOT EXISTS customers ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, image_url VARCHAR(255) NOT NULL ); `;constinsertedCustomers=awaitPromise.all(customers.map((customer)=>sql` INSERT INTO customers (id, name, email, image_url) VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}) ON CONFLICT (id) DO NOTHING; `,),);returninsertedCustomers;}asyncfunctionseedRevenue(sql: postgres.TransactionSql){awaitsql` CREATE TABLE IF NOT EXISTS revenue ( month VARCHAR(4) NOT NULL UNIQUE, revenue INT NOT NULL ); `;constinsertedRevenue=awaitPromise.all(revenue.map((rev)=>sql` INSERT INTO revenue (month, revenue) VALUES (${rev.month}, ${rev.revenue}) ON CONFLICT (month) DO NOTHING; `,),);returninsertedRevenue;}exportasyncfunctionGET(){try{constresult=awaitsql.begin((sql)=>[seedUsers(sql),seedCustomers(sql),seedInvoices(sql),seedRevenue(sql),]);returnResponse.json({message: 'Database seeded successfully'});}catch(error){returnResponse.json({ error },{status: 500});}}
Replacing the /seed/route.tsx with above code fixes the issue and /seed script completes successfully
@denolia I believe the PR is addressing a different issue due to unique constraint violation with error code 23505. In my case I had error code 26000 and had no issues with unique constraint violation. Also if you notice in my fix above, the sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; part is left untouched and still exists in every seed function and the fix works.
Since I do not see others complaining about my issue, I believe my issue is due to some weird interaction with supabase and using the global sql instance. Making use of the scoped sql instance makes the problem go away for me without touching any other parts of the code.
Lesson: App Router
Chapter: 6
Database provider: Supabase (It is likely the error will occur with other providers as well)
Seeding the database with /seed fails and returns the following response with 500 status.

Possible cause
From what I understand the seed functions do not make use of the scoped
sql
instance provided bysql.begin
and instead end up using the sql instance declared at the top of/seed/route.tsx
file.Fix
Update
/seed/route.tsx
to make use of the scopedsql
instance supplied bysql.begin
Replacing the /seed/route.tsx with above code fixes the issue and /seed script completes successfully
References:
https://www.npmjs.com/package/postgres#transactions
The text was updated successfully, but these errors were encountered: