Nullable self-reference table relation

typescript: No overload matches this call.
Overload 1 of 2, '(left: Aliased<null>, right: AnyColumn | Placeholder<string, any> | SQLWrapper | null): SQL<unknown>', gave the following error.
Argument of type 'MySqlInt<{ tableName: "cuentas"; name: "padre_id"; data: number; driverParam: string | number; notNull: false; hasDefault: false; }>' is not assignable to parameter of type 'Aliased<null>'.
Type 'MySqlInt<{ tableName: "cuentas"; name: "padre_id"; data: number; driverParam: string | number; notNull: false; hasDefault: false; }>' is missing the following properties from type 'Aliased<null>': sql, fieldAlias, getSQL
Overload 2 of 2, '(left: MySqlInt<{ tableName: "cuentas"; name: "padre_id"; data: number; driverParam: string | number; notNull: false; hasDefault: false; }>, right: number | AnyColumn | Placeholder<...> | SQLWrapper): SQL<...>', gave the following error.
Argument of type 'null' is not assignable to parameter of type 'number | AnyColumn | Placeholder<string, any> | SQLWrapper'. [2769]
typescript: No overload matches this call.
Overload 1 of 2, '(left: Aliased<null>, right: AnyColumn | Placeholder<string, any> | SQLWrapper | null): SQL<unknown>', gave the following error.
Argument of type 'MySqlInt<{ tableName: "cuentas"; name: "padre_id"; data: number; driverParam: string | number; notNull: false; hasDefault: false; }>' is not assignable to parameter of type 'Aliased<null>'.
Type 'MySqlInt<{ tableName: "cuentas"; name: "padre_id"; data: number; driverParam: string | number; notNull: false; hasDefault: false; }>' is missing the following properties from type 'Aliased<null>': sql, fieldAlias, getSQL
Overload 2 of 2, '(left: MySqlInt<{ tableName: "cuentas"; name: "padre_id"; data: number; driverParam: string | number; notNull: false; hasDefault: false; }>, right: number | AnyColumn | Placeholder<...> | SQLWrapper): SQL<...>', gave the following error.
Argument of type 'null' is not assignable to parameter of type 'number | AnyColumn | Placeholder<string, any> | SQLWrapper'. [2769]
My schema
import { relations } from "drizzle-orm";
import { int, mysqlTable, serial, varchar } from "drizzle-orm/mysql-core";

export const account = mysqlTable("account", {
id: serial("id").primaryKey(),
name: varchar("name", { length: 256 }),
parentId: int("parentId"),
});

export const accountRelations = relations(account, ({ one }) => ({
parent: one(account, {
fields: [account.parentId],
references: [account.id],
}),
}));
import { relations } from "drizzle-orm";
import { int, mysqlTable, serial, varchar } from "drizzle-orm/mysql-core";

export const account = mysqlTable("account", {
id: serial("id").primaryKey(),
name: varchar("name", { length: 256 }),
parentId: int("parentId"),
});

export const accountRelations = relations(account, ({ one }) => ({
parent: one(account, {
fields: [account.parentId],
references: [account.id],
}),
}));
Index.ts
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

import * as schema from "./schema";

const poolConnection = mysql.createPool({
host: "host",
user: "user",
database: "database",
password: "password",
});

export const db = drizzle(poolConnection, { schema });

await db.query.account.findMany({
where: (account, { eq }) => eq(account.parentId, null) // ERRORS HERE
})
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

import * as schema from "./schema";

const poolConnection = mysql.createPool({
host: "host",
user: "user",
database: "database",
password: "password",
});

export const db = drizzle(poolConnection, { schema });

await db.query.account.findMany({
where: (account, { eq }) => eq(account.parentId, null) // ERRORS HERE
})
I haven't put a .notNull() anywhere
3 Replies
Andrii Sherman
Andrii Sherman•2y ago
👀 yeah, can reproduce it just saw an issue on GH thanks a lot will check and fix that asap
pedro77
pedro77OP•2y ago
Thank you
Dan
Dan•2y ago
@pedro77 you cannot compare a column to null in SQL, it will always return false, thus it's not allowed in eq() use isNull() instead so it's not a bug in SQL terms: <column> = null -> always false, even if the column value is null <column> is null -> true if the column value is null
Want results from more Discord servers?
Add your server