"Seems like the schema generic is missing - did you forget to add it to your DB type?"

how to fix this... i need to use db.query
7 Replies
Hussein
HusseinOP15mo ago
? do i need to pass {schema} to drizzle()? its giving me this huge error
Type 'BetterSQLite3Database<typeof import("/home/h/Desktop/web-app/src/schema")>' is not assignable to type 'BetterSQLite3Database'.
The types of '_.schema' are incompatible between these types.
Type 'ExtractTablesWithRelations<typeof import("/home/h/Desktop/web-app/src/schema")> | undefined' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>> | undefined'.
Type 'ExtractTablesWithRelations<typeof import("/home/h/Desktop/web-app/src/schema")>' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>>'.
Property 'users' is incompatible with index signature.
Type '{ tsName: "users"; dbName: "users"; columns: { id: SQLiteColumn<{ name: "id"; tableName: "users"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; username: SQLiteColumn<...>; password: SQLiteColumn<......' is not assignable to type '{ tsName: string; dbName: never; columns: never; relations: Record<string, Relation<string>>; primaryKey: AnyColumn[]; }'.
Types of property 'dbName' are incompatible.
Type 'string' is not assignable to type 'never'.ts(2322)
Type 'BetterSQLite3Database<typeof import("/home/h/Desktop/web-app/src/schema")>' is not assignable to type 'BetterSQLite3Database'.
The types of '_.schema' are incompatible between these types.
Type 'ExtractTablesWithRelations<typeof import("/home/h/Desktop/web-app/src/schema")> | undefined' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>> | undefined'.
Type 'ExtractTablesWithRelations<typeof import("/home/h/Desktop/web-app/src/schema")>' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>>'.
Property 'users' is incompatible with index signature.
Type '{ tsName: "users"; dbName: "users"; columns: { id: SQLiteColumn<{ name: "id"; tableName: "users"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; username: SQLiteColumn<...>; password: SQLiteColumn<......' is not assignable to type '{ tsName: string; dbName: never; columns: never; relations: Record<string, Relation<string>>; primaryKey: AnyColumn[]; }'.
Types of property 'dbName' are incompatible.
Type 'string' is not assignable to type 'never'.ts(2322)
this is my schema
import {sqliteTable, text} from 'drizzle-orm/sqlite-core'
import {createId} from '@paralleldrive/cuid2'
import {relations} from 'drizzle-orm'

const id = text('id', {length: 128})
.primaryKey()
.$defaultFn(() => createId())

export const users = sqliteTable('users', {
id,
username: text('username', {length: 255}).unique().notNull(),
password: text('password', {length: 255}).notNull(),
profilePicture: text('profile_picture'),
profilePictureAlt: text('pfp_alt')
})

export const usersRelations = relations(users, ({many}) => ({
posts: many(friends)
}))

export const friends = sqliteTable('posts', {
userId: text('user_id'),
friendId: text('friend_id')
})

export const friendsRelations = relations(friends, ({one}) => ({
user: one(users, {
fields: [friends.userId, friends.friendId],
references: [users.id, users.id]
})
}))
import {sqliteTable, text} from 'drizzle-orm/sqlite-core'
import {createId} from '@paralleldrive/cuid2'
import {relations} from 'drizzle-orm'

const id = text('id', {length: 128})
.primaryKey()
.$defaultFn(() => createId())

export const users = sqliteTable('users', {
id,
username: text('username', {length: 255}).unique().notNull(),
password: text('password', {length: 255}).notNull(),
profilePicture: text('profile_picture'),
profilePictureAlt: text('pfp_alt')
})

export const usersRelations = relations(users, ({many}) => ({
posts: many(friends)
}))

export const friends = sqliteTable('posts', {
userId: text('user_id'),
friendId: text('friend_id')
})

export const friendsRelations = relations(friends, ({one}) => ({
user: one(users, {
fields: [friends.userId, friends.friendId],
references: [users.id, users.id]
})
}))
Angelelz
Angelelz15mo ago
How are you defining the drizzle object?
Hussein
HusseinOP15mo ago
import * as schema from '../../schema'
import Database from 'better-sqlite3'
import {migrate} from 'drizzle-orm/better-sqlite3/migrator'
import {drizzle, type BetterSQLite3Database} from 'drizzle-orm/better-sqlite3'

const sqlite = new Database('sqlite.db')
export const database: BetterSQLite3Database = drizzle(sqlite, {schema})
import * as schema from '../../schema'
import Database from 'better-sqlite3'
import {migrate} from 'drizzle-orm/better-sqlite3/migrator'
import {drizzle, type BetterSQLite3Database} from 'drizzle-orm/better-sqlite3'

const sqlite = new Database('sqlite.db')
export const database: BetterSQLite3Database = drizzle(sqlite, {schema})
i changed this schema according to https://discord.com/channels/1043890932593987624/1111572543556550738/1148828114345021510 so now its
import {primaryKey, sqliteTable, text} from 'drizzle-orm/sqlite-core'
import {createId} from '@paralleldrive/cuid2'
import {relations} from 'drizzle-orm'

const id = text('id', {length: 128})
.primaryKey()
.$defaultFn(() => createId())

export const users = sqliteTable('users', {
id,
username: text('username', {length: 255}).unique().notNull(),
password: text('password', {length: 255}).notNull(),
profilePicture: text('profile_picture'),
profilePictureAlt: text('pfp_alt')
})

export const usersRelations = relations(users, ({many}) => ({
follows: many(follows, {relationName: 'follows'}),
followers: many(follows, {relationName: 'followers'})
}))

export const follows = sqliteTable(
'follows',
{
followerId: id,
followeeId: id
},
(t) => ({
pk: primaryKey(t.followerId, t.followeeId)
})
)

export const followsRelations = relations(follows, ({one}) => ({
follower: one(users, {
fields: [follows.followerId],
references: [users.id],
relationName: 'follows'
}),
followee: one(users, {
fields: [follows.followeeId],
references: [users.id],
relationName: 'followers'
})
}))
import {primaryKey, sqliteTable, text} from 'drizzle-orm/sqlite-core'
import {createId} from '@paralleldrive/cuid2'
import {relations} from 'drizzle-orm'

const id = text('id', {length: 128})
.primaryKey()
.$defaultFn(() => createId())

export const users = sqliteTable('users', {
id,
username: text('username', {length: 255}).unique().notNull(),
password: text('password', {length: 255}).notNull(),
profilePicture: text('profile_picture'),
profilePictureAlt: text('pfp_alt')
})

export const usersRelations = relations(users, ({many}) => ({
follows: many(follows, {relationName: 'follows'}),
followers: many(follows, {relationName: 'followers'})
}))

export const follows = sqliteTable(
'follows',
{
followerId: id,
followeeId: id
},
(t) => ({
pk: primaryKey(t.followerId, t.followeeId)
})
)

export const followsRelations = relations(follows, ({one}) => ({
follower: one(users, {
fields: [follows.followerId],
references: [users.id],
relationName: 'follows'
}),
followee: one(users, {
fields: [follows.followeeId],
references: [users.id],
relationName: 'followers'
})
}))
Angelelz
Angelelz15mo ago
Try this:
export const database = drizzle(sqlite, {schema})
// or
export const database: BetterSQLite3Database<typeof schema> = drizzle(sqlite, {schema})
export const database = drizzle(sqlite, {schema})
// or
export const database: BetterSQLite3Database<typeof schema> = drizzle(sqlite, {schema})
Hussein
HusseinOP15mo ago
thanks can you help me with this schema
DrizzleError: Failed to run the query 'CREATE TABLE `follows` (
`id` text(128) PRIMARY KEY NOT NULL,
PRIMARY KEY(`id`, `id`)
);
': table "follows" has more than one primary key
DrizzleError: Failed to run the query 'CREATE TABLE `follows` (
`id` text(128) PRIMARY KEY NOT NULL,
PRIMARY KEY(`id`, `id`)
);
': table "follows" has more than one primary key
Kilisei
Kilisei14mo ago
@Angelelz you saved my sanity
Want results from more Discord servers?
Add your server