Typescript error that doesn't make a whole lot of sense when calling db.select or db.insert

This is my account model
export const accounts = mysqlTable("accounts", {
id: nanoid("id", {}).primaryKey(),
userId: varchar("userId", { length: 25 }),
displayName: text("displayName"),
metadata: json("metadata"),
externalId: varchar("externalId", { length: 25 }),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt"),
});
export const accounts = mysqlTable("accounts", {
id: nanoid("id", {}).primaryKey(),
userId: varchar("userId", { length: 25 }),
displayName: text("displayName"),
metadata: json("metadata"),
externalId: varchar("externalId", { length: 25 }),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt"),
});
This issue happens to both my db.insert and db.select requests. Any guidance would be much appreciated.
15 Replies
Andrii Sherman
Andrii Sherman•2y ago
eq() should get column as first param And value for it as second
quan2796
quan2796•2y ago
Thanks for that @Andrew Sherman , the original issue with accounts didn't go away unfortunately
Andrii Sherman
Andrii Sherman•2y ago
Can you please show part where you create db from drizzle() And where did you import drizzle from
quan2796
quan2796•2y ago
import { Config } from "sst/node/config";

import { connect } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";

export const connection = connect({
host: Config.PLANETSCALE_HOST,
username: Config.PLANETSCALE_USERNAME,
password: Config.PLANETSCALE_PASSWORD,
});
export const db = drizzle(connection);
import { Config } from "sst/node/config";

import { connect } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";

export const connection = connect({
host: Config.PLANETSCALE_HOST,
username: Config.PLANETSCALE_USERNAME,
password: Config.PLANETSCALE_PASSWORD,
});
export const db = drizzle(connection);
^ this is the db.ts file Then in my accounts file I have
import { accounts, NewAccount } from "@models/account";
import { db } from "@connectors/db";
import { eq } from "drizzle-orm";

export const createAccount = async (newAccount: NewAccount) => {
await db.insert(accounts).values(newAccount);
};

export const getAccountByExternalId = async (externalId: string) => {
return db.select({
id: accounts.id,
}).from(accounts).where(eq(accounts.externalId, externalId));
};
import { accounts, NewAccount } from "@models/account";
import { db } from "@connectors/db";
import { eq } from "drizzle-orm";

export const createAccount = async (newAccount: NewAccount) => {
await db.insert(accounts).values(newAccount);
};

export const getAccountByExternalId = async (externalId: string) => {
return db.select({
id: accounts.id,
}).from(accounts).where(eq(accounts.externalId, externalId));
};
version: "drizzle-orm": "^0.23.13"
Andrii Sherman
Andrii Sherman•2y ago
let me test it locally so It works for me could you please send your table schema + imports for it and nanoId implementation? maybe something wrong there because everything else was taken from you example
quan2796
quan2796•2y ago
accounts.ts
import { json, text, varchar } from "drizzle-orm/mysql-core";
import { InferModel } from "drizzle-orm";
import { nanoid } from "./utils";

import { timestamp } from "drizzle-orm/mysql-core";
import { table } from "./table";
export const accounts = table("accounts", {
id: nanoid("id", {}).primaryKey(),
userId: varchar("userId", { length: 32 }).notNull(),
displayName: text("displayName").notNull(),
metadata: json("metadata"),
externalId: varchar("externalId", { length: 36 }),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt"),
});

export type Account = InferModel<typeof accounts>;
export type NewAccount = InferModel<typeof accounts, "insert">;
import { json, text, varchar } from "drizzle-orm/mysql-core";
import { InferModel } from "drizzle-orm";
import { nanoid } from "./utils";

import { timestamp } from "drizzle-orm/mysql-core";
import { table } from "./table";
export const accounts = table("accounts", {
id: nanoid("id", {}).primaryKey(),
userId: varchar("userId", { length: 32 }).notNull(),
displayName: text("displayName").notNull(),
metadata: json("metadata"),
externalId: varchar("externalId", { length: 36 }),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt"),
});

export type Account = InferModel<typeof accounts>;
export type NewAccount = InferModel<typeof accounts, "insert">;
table.ts
import { mysqlTable } from "drizzle-orm/mysql-core";

export const table = mysqlTable;
import { mysqlTable } from "drizzle-orm/mysql-core";

export const table = mysqlTable;
nanoid
import { customType } from "drizzle-orm/mysql-core";
import { nanoid as nanoidLib } from "nanoid";

export const nanoid = customType<{
data: string;
notNull: true;
default: true;
}>({
dataType() {
return "varchar(20)";
},
toDriver(value): string {
return typeof value === "undefined" ? nanoidLib() : value;
},
});
import { customType } from "drizzle-orm/mysql-core";
import { nanoid as nanoidLib } from "nanoid";

export const nanoid = customType<{
data: string;
notNull: true;
default: true;
}>({
dataType() {
return "varchar(20)";
},
toDriver(value): string {
return typeof value === "undefined" ? nanoidLib() : value;
},
});
I believe that should be everything
Andrii Sherman
Andrii Sherman•2y ago
yes thanks still works another question do you have 1 drizzle-orm installed? or are you using monorepo and having several drizzle-orm instances?
quan2796
quan2796•2y ago
I am using monorepo but I only have drizzle-orm only in 1 package.json whelp, other than it looking ugly in my IDE it's not preventing my code from executing so it's fine for now I guess Thanks for looking into it @Andrew Sherman
Andrii Sherman
Andrii Sherman•2y ago
it shouldn't be this way
quan2796
quan2796•2y ago
let me try a different IDE
Andrii Sherman
Andrii Sherman•2y ago
I guess you can try installing drizzle-orm globally in monorepo to check if it will work but if you have only 1 drizzle-orm installed - no errors should be displayed
quan2796
quan2796•2y ago
Very intersting, doesn't happen on Vscode
quan2796
quan2796•2y ago
Oh damn, I restarted my webstorm IDE and now the error is also gone.
quan2796
quan2796•2y ago
I dunno how it worked but it did Thanks again @Andrew Sherman
Andrii Sherman
Andrii Sherman•2y ago
🫡
Want results from more Discord servers?
Add your server