Cannot insert rows with drizzle

I have the following schema
export const user = pgTable(
'User',
{
username: varchar('username', { length: 50 }).notNull(),
password: varchar('password', { length: 255 }).notNull(),
id: uuid('id').primaryKey().notNull().defaultRandom(),
},
(table) => {
return {
usernameKey: uniqueIndex('User_username_key').on(table.username),
}
},
)
export const user = pgTable(
'User',
{
username: varchar('username', { length: 50 }).notNull(),
password: varchar('password', { length: 255 }).notNull(),
id: uuid('id').primaryKey().notNull().defaultRandom(),
},
(table) => {
return {
usernameKey: uniqueIndex('User_username_key').on(table.username),
}
},
)
and I am trying to seed my database with test rows like this
import { db } from './db'
import { user } from './schema'
import { hash } from 'bcrypt'

async function main() {
try {
console.log('Seeding database')
const password = await hash('password', 10)
const insertedUser = await db
.insert(user)
.values({
username: 'jorge',
password,
})
.returning()
.execute()
console.log('Database seeded')
} catch (e) {
console.error('Error seeding database', e)
}
}

;(async () => {
await main()
})()
import { db } from './db'
import { user } from './schema'
import { hash } from 'bcrypt'

async function main() {
try {
console.log('Seeding database')
const password = await hash('password', 10)
const insertedUser = await db
.insert(user)
.values({
username: 'jorge',
password,
})
.returning()
.execute()
console.log('Database seeded')
} catch (e) {
console.error('Error seeding database', e)
}
}

;(async () => {
await main()
})()
However, it seems like it does not get past the insert as the "Database seeded" message is never logged in the console. Is there something I am doing wrong?
1 Reply
Jorge
Jorge6mo ago
After investigating, I discovered that it only fails in the seed file. I can successfully insert rows if I run that code in main.ts file
Want results from more Discord servers?
Add your server