Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pnpm db:seed is giving me a duplicate key value error of users_email_unique. #66

Open
TommyJackson85 opened this issue Oct 20, 2024 · 5 comments

Comments

@TommyJackson85
Copy link

pnpm db:seed is giving me a duplicate key value error of users_email_unique. Any suggestions on what to change in the functions?

Seed process failed: PostgresError: duplicate key value violates unique constraint "users_email_unique" at ErrorResponse (/Users/thomasjackson/Desktop/venginez-templates/next-saas-starter/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/connection.js:788:26) at handle (/Users/thomasjackson/Desktop/venginez-templates/next-saas-starter/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/connection.js:474:6) at TLSSocket.data (/Users/thomasjackson/Desktop/venginez-templates/next-saas-starter/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/connection.js:315:9) at TLSSocket.emit (node:events:518:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5) at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) { severity_local: 'ERROR', severity: 'ERROR', code: '23505', detail: 'Key (email)=([email protected]) already exists.', schema_name: 'public', table_name: 'users', constraint_name: 'users_email_unique', file: 'nbtinsert.c', line: '666', routine: '_bt_check_unique'

@leerob
Copy link
Collaborator

leerob commented Oct 20, 2024

Have you already ran the script? You could use db:studio to view the contents and then delete that user.

@TommyJackson85
Copy link
Author

ill try db studio

@TommyJackson85
Copy link
Author

I just checked and theres one user of [email protected] email. so I don't see why there's an issue with it. i am afraid to delete it because I want to use that user for testing in general.
In an attempt to delete it too I am getting this error
'update or delete on table "users" violates foreign key constraint "activity_logs_user_id_users_id_fk" on table "activity_logs"'
and its asking me to report it drizzle studios github.

@TommyJackson85
Copy link
Author

is it possible theres a cookies related issues with this? can I check my database by the terminal to get more accurate info? any help would be great

@sanjomathew09
Copy link

const postgres = require('postgres'); // Ensure this is your Postgres library

const sql = postgres(process.env.DATABASE_URL);

async function seedUsers() {
try {
const email = '[email protected]';

// Check if the user already exists
const existingUser = await sql`
  SELECT * FROM users WHERE email = ${email};
`;

if (existingUser.length === 0) {
  // Insert user only if it doesn't exist
  await sql`
    INSERT INTO users (email, name, password) 
    VALUES (${email}, 'Test User', 'hashedpassword');
  `;
  console.log('User seeded successfully');
} else {
  console.log(`User with email ${email} already exists. Skipping...`);
}

} catch (error) {
console.error('Error seeding users:', error);
} finally {
sql.end(); // Close the connection
}
}

seedUsers();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants