DT
Drizzle Team•4mo ago
ivan

sqliteTable infer optional vs required fields

I am trying to create optional fields on my model but no matter what I do the drizzle is inferring all the properties as required. See the example below:
const example = sqliteTable("example", {
id: text("id").primaryKey(),
required: text("required").notNull(),
optional: text("optional"),
});

type NewExample = typeof example.$inferInsert;
// resolves in:
// type NewExample = {
// id: string;
// required: string;
// }

type Example = typeof example.$inferSelect;
// resolves in:
// type Example = {
// id: string;
// required: string;
// optional: string;
// }

// my expectation:
// type Example = {
// id: string;
// required: string;
// optional?: string;
// }
//
// or
//
// type Example = {
// id: string;
// required: string;
// optional: string | undefined;
// }
const example = sqliteTable("example", {
id: text("id").primaryKey(),
required: text("required").notNull(),
optional: text("optional"),
});

type NewExample = typeof example.$inferInsert;
// resolves in:
// type NewExample = {
// id: string;
// required: string;
// }

type Example = typeof example.$inferSelect;
// resolves in:
// type Example = {
// id: string;
// required: string;
// optional: string;
// }

// my expectation:
// type Example = {
// id: string;
// required: string;
// optional?: string;
// }
//
// or
//
// type Example = {
// id: string;
// required: string;
// optional: string | undefined;
// }
Is there any way to achieve expected behaviour with the drizzle orm?
6 Replies
rphlmr âš¡
rphlmr ⚡•4mo ago
👋 Can you share your ts config?
No description
No description
rphlmr âš¡
rphlmr ⚡•4mo ago
Check if you have strict to true
ivan
ivanOP•4mo ago
Seems that strict is unrelated to this since it is used for push as stated here: https://orm.drizzle.team/kit-docs/config-reference#strict Nevertheless this is my config:
import type { Config } from "drizzle-kit";

export default {
schema: "./src/app/schema.ts",
out: "./db/migrations",
dialect: "sqlite",
dbCredentials: {
url: "./data.db",
},
} satisfies Config;
import type { Config } from "drizzle-kit";

export default {
schema: "./src/app/schema.ts",
out: "./db/migrations",
dialect: "sqlite",
dbCredentials: {
url: "./data.db",
},
} satisfies Config;
Also here are my drizzle versions:
Drizzle ORM - Config Reference
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
rphlmr âš¡
rphlmr ⚡•4mo ago
oh sorry I mean strict in your tsconfig file
{
"compilerOptions": {
"strict": true, // this
...
}
}
{
"compilerOptions": {
"strict": true, // this
...
}
}
ivan
ivanOP•4mo ago
Perfect! That's exactly what I was missing. Thank you very much. Do I mark this thread as resolved now or something?
rphlmr âš¡
rphlmr ⚡•4mo ago
Yeah if you want!
Want results from more Discord servers?
Add your server