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

Qquan27965/8/2023
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.
ASAndrii Sherman5/8/2023
eq() should get column as first param And value for it as second
Qquan27965/8/2023
Thanks for that @Andrew Sherman , the original issue with accounts didn't go away unfortunately
ASAndrii Sherman5/8/2023
Can you please show part where you create db from drizzle() And where did you import drizzle from
Qquan27965/8/2023
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"
ASAndrii Sherman5/8/2023
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
Qquan27965/8/2023
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
ASAndrii Sherman5/8/2023
yes thanks still works another question do you have 1 drizzle-orm installed? or are you using monorepo and having several drizzle-orm instances?
Qquan27965/8/2023
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
ASAndrii Sherman5/8/2023
it shouldn't be this way
Qquan27965/8/2023
let me try a different IDE
ASAndrii Sherman5/8/2023
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
Qquan27965/8/2023
Very intersting, doesn't happen on Vscode
Qquan27965/8/2023
Oh damn, I restarted my webstorm IDE and now the error is also gone.
Qquan27965/8/2023
I dunno how it worked but it did Thanks again @Andrew Sherman
ASAndrii Sherman5/8/2023
🫡

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Are default values not transferred over via drizzle-zod?It seems like when I use a `default([])` in my `schema` file that when I create a Zod object, I haveReplace on InsertI can see that there is a `.ignore()` option added to insert, but is there an equivalent `.replace()Is there a way to modify the select() on an existing query ?Let's say I have this query that I pass to a function ```ts const query = db.select().from(myTable)The inferred type of '<tableName>' cannot be named without a reference to '.pnpm/[email protected]I'm trying to use the `drizzle-zod` but I keep getting this error... Any idea on where it comes froMigration failure on fresh DBI'm currently experiencing a migration failure on a fresh Postgres DB with both postgres.js and nodeUsing Vercel Postgres and developing locallyTo confirm, if I want to use Vercel Postgres in production and a local Postgres DB for development, Custom Type interpreted as StringHello everyone, I am having some issues with getting Drizzle to work with PostGIS, I am trying to haCan I use queryBuilder for inserts?My scenario is that I want to read some rows from a database (prod), and generate a SQL file that weI ran introspect:pg to initialize my schema, then created a new migration. How should I deploy this?The remote database, right now, doesn't have the `drizzle.__drizzle_migrations` table. It'd be nice Transaction rollbackHello guys. Kind of dumb question but should I cover body of transaction in try/catch and call rollberror creating relationshipI'm having trouble creating a relationship in MySQL using drizzle-orm, I'm running `npx drizzle-kit define default value for arrayI work with a PostgreSQL database. Here is my Schema: export const user = pgTable( 'auth_user',TS query types dont match + no return typesI'm trying to query data from my db after importing the Item table from my schema. `const data = awaCan't generate migrations because of top level awaitThis only recently started happening.Here is my `drizzle.config.json` : ```{ "out": "./migratError types with custom schemaHi, I'm trying to do a general function to paginate my querys, but I'm having some problems with theTrying to write next-auth adapterblahblahblahrober AKA @Dan Kochetov helped me get most of the way to having a next-auth adapter doneBeen getting this error while using the libsql client. The code works though. Any ideas. Thanks! 🙂I get the error from the screenshot every time I access my db client, but the code works fine thoughSelect wildcardI'm sure someone's asked this before but I can't find it. Is there not a way to do a `.select()` wiRangeError: Maximum call stack size exceeded - When I try to INSERTHi, I am getting `RangeError: Maximum call stack size exceeded` when I try to INSERT something in tTables are not being generatedHey 👋 I'm probably missing something obvious but with the setup I have (shown in the image) drizzl