drizzle-team / drizzle-team/drizzle-orm
[BUG]: pushSchema fails with 'Cannot read properties of undefined (reading reduce)' - execute() return format mismatch
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
`pushSchema` from `drizzle-kit/api-postgres` fails with "Cannot read properties of undefined (reading 'reduce')" when used with drizzle-orm's postgres-js driver.
## What is the undesired behavior?
When calling `pushSchema(schema, db)` where `db` is created via `drizzle(connectionString)` from `drizzle-orm/postgres-js`, the function throws:
```
TypeError: Cannot read properties of undefined (reading 'reduce')
at fromDatabase (file:///node_modules/drizzle-kit/api.mjs:46474:53)
```
## Root cause
The issue is in how `pushSchema` creates its database adapter. It wraps the drizzle instance's `execute` method expecting it to return `{ rows: [...] }`:
```javascript
// In drizzle-kit's createDrizzleDbFromPostgresJsDatabase:
async query(sql) {
const result = await drizzle3.execute(import_drizzle_orm9.sql.raw(sql));
return { rows: result.rows }; // result.rows is undefined!
}
```
However, `drizzle-orm`'s postgres-js driver returns the array directly from `execute()`, not an object with a `rows` property:
```javascript
// What drizzle-orm actually returns:
await db.execute(sql`SELECT ...`)
// Returns: [{ oid: 13293, name: 'information_schema' }, ...]
// What drizzle-kit expects:
// { rows: [{ oid: 13293, name: 'information_schema' }, ...] }
```
This causes `result.rows` to be `undefined`, which then fails when `.reduce()` is called on it in `fromDatabase`.
## Steps to reproduce
1. Create a new project with these dependencies:
```json
{
"dependencies": {
"drizzle-kit": "1.0.0-beta.12-a5629fb",
"drizzle-orm": "1.0.0-beta.12-a5629fb",
"postgres": "^3.4.5"
}
}
```
2. Create a schema file:
```typescript
// schema.ts
import { pgTable, text, uuid, boolean, integer, timestamp } from 'drizzle-orm/pg-core';
export const observations = pgTable('observations', {
id: uuid('id').primaryKey().defaultRandom(),
monitor: text('monitor').notNull(),
observedAt: timestamp('observed_at', { withTimezone: true }).notNull(),
success: boolean('success').notNull(),
responseTimeMs: integer('response_time_ms'),
errorMessage: text('error_message'),
});
```
3. Try to push the schema:
```typescript
import { drizzle } from 'drizzle-orm/postgres-js';
import { pushSchema } from 'drizzle-kit/api-postgres';
import * as schema from './schema.js';
const db = drizzle('postgresql://user:pass@localhost:5432/db');
const result = await pushSchema(schema, db);
await result.apply();
```
4. Observe the error:
```
TypeError: Cannot read properties of undefined (reading 'reduce')
at fromDatabase (file:///node_modules/drizzle-kit/api.mjs:46474:53)
```
## What is the desired result?
`pushSchema` should work with the drizzle instance created by `drizzle-orm/postgres-js`. The adapter in drizzle-kit should handle the actual return format of `execute()`.
## Environment
- Database: PostgreSQL 18
- Driver: postgres (postgres.js)
- Runtime: Node.js 22
- TypeScript: 5.7
Contributor guide
Assessment
This issue has not been assessed yet.