Type error: column type does not match
What causes this
Drizzle ORM is fully type-safe — your TypeScript types are derived from your schema definitions. When you try to insert or query data that doesn’t match the column types in your schema, TypeScript catches it at compile time.
Common causes:
- Inserting a string into a number column or vice versa
- Missing required columns in an insert
- Schema definition doesn’t match the actual database column type
- Using the wrong Drizzle column helper (e.g.,
text()vsvarchar()) - After a migration, the schema file wasn’t updated to match
Fix 1: Match your insert data to the schema
// Schema
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
age: integer('age'),
email: text('email').notNull(),
});
// ❌ Type error — age expects number, not string
await db.insert(users).values({
name: 'Alice',
age: '25',
email: 'alice@example.com',
});
// ✅ Correct types
await db.insert(users).values({
name: 'Alice',
age: 25,
email: 'alice@example.com',
});
Fix 2: Use the correct column type helper
// ❌ Using integer() but the DB column is actually a string
const posts = pgTable('posts', {
viewCount: integer('view_count'), // DB has varchar
});
// ✅ Match the actual DB type
const posts = pgTable('posts', {
viewCount: text('view_count'), // If DB stores it as text
});
Common Drizzle column types:
text()/varchar()→ stringsinteger()/serial()→ numbersboolean()→ true/falsetimestamp()→ datesjson()/jsonb()→ objects
Fix 3: Handle nullable columns
const users = pgTable('users', {
bio: text('bio'), // nullable by default
});
// The inferred type is: string | null
// ❌ Type error if you treat it as always string
const bioLength = user.bio.length;
// ✅ Handle null
const bioLength = user.bio?.length ?? 0;
Fix 4: Regenerate types after migration
After changing your database schema:
# Generate a new migration
npx drizzle-kit generate
# Push changes to the database
npx drizzle-kit push
# If using introspection (pull mode)
npx drizzle-kit introspect
Make sure your schema file matches what’s actually in the database.
Fix 5: Use $inferInsert and $inferSelect
Let Drizzle generate the types for you instead of writing them manually:
import { users } from './schema';
type NewUser = typeof users.$inferInsert;
type User = typeof users.$inferSelect;
// Now TypeScript knows exactly what fields and types are expected
const newUser: NewUser = {
name: 'Alice',
email: 'alice@example.com',
};
Related resources
How to prevent it
- Always use
$inferInsertand$inferSelectinstead of manually typing your data objects - Run
drizzle-kit pushordrizzle-kit generateafter every schema change - Keep your schema file as the single source of truth — don’t modify the database directly
- Use
drizzle-kit studioto inspect your database and verify column types match your schema