Why can't I insert fields that have already been defined in the table?

This is my user schema
export const users = pgTable(
'users',
{
id: uuid('id').primaryKey().defaultRandom(),
email: text('email').unique().notNull(),
name: text('name').notNull(),
password: text('password'),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
},
(t) => [uniqueIndex('email_idx').on(t.email)],
);
export const users = pgTable(
'users',
{
id: uuid('id').primaryKey().defaultRandom(),
email: text('email').unique().notNull(),
name: text('name').notNull(),
password: text('password'),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
},
(t) => [uniqueIndex('email_idx').on(t.email)],
);
This is my seed file
const pool = new Pool({
connectionString: process.env.DATABASE_URL!,
ssl: true,
});

const db = drizzle(pool, { schema }) as NodePgDatabase<typeof schema>;

async function main() {
const userIds = await Promise.all(
Array.from({ length: 10 }).map(async () => {
const user = await db
.insert(schema.users)
.values({
email: faker.internet.email(),
name: faker.person.firstName() + ' ' + faker.person.lastName(),
})
.returning();
return user[0].id;
}),
);
}

main()
.then()
.catch((err) => {
console.log('err:', err);
process.exit(0);
});
const pool = new Pool({
connectionString: process.env.DATABASE_URL!,
ssl: true,
});

const db = drizzle(pool, { schema }) as NodePgDatabase<typeof schema>;

async function main() {
const userIds = await Promise.all(
Array.from({ length: 10 }).map(async () => {
const user = await db
.insert(schema.users)
.values({
email: faker.internet.email(),
name: faker.person.firstName() + ' ' + faker.person.lastName(),
})
.returning();
return user[0].id;
}),
);
}

main()
.then()
.catch((err) => {
console.log('err:', err);
process.exit(0);
});
When I execute the seed.ts file, it works ! It inserts 10 pieces of data into the database. But when I tried to add the password field, the typescript prompted me that there was no password field in the users table. May I ask why this is? Is there a problem with the type derivation of NodePgDatabase<typeof schema> Is there any expert who can help answer this? Thank you very much!
No description
4 Replies
RWOverdijk
RWOverdijk2mo ago
I am not an expert, sorry. But looking at this I feel it has something to do with password being nullable maybe. It seems to be the only difference with the other fields. It's not actually helpful, but it is something you can try to narrow down the issue.
xxgw
xxgwOP2mo ago
Thank you for your reply😆 , but in my work, password fields are allowed to be empty. I don't understand why fields without the notNull() modifier won't have type prompts
RWOverdijk
RWOverdijk2mo ago
Me neither, but maybe try it to narrow it down If the issue is in the missing notNull, we know where to look for a solution
RWOverdijk
RWOverdijk2mo ago
https://github.com/drizzle-team/drizzle-orm/issues/4219 from another question in here, seems the same.
GitHub
[BUG]: (drizzle-orm/sqlite-core) SQLite Schemas do not support null...
Report hasn't been filed before. I have verified that the bug I'm about to report hasn't been filed before. What version of drizzle-orm are you using? 0.40.0 What version of drizzle-kit...

Did you find this page helpful?