pvman
pvman
Explore posts from servers
DTDrizzle Team
Created by pvman on 6/10/2024 in #help
Cannot set null value to FK - null value in column "X" of relation "Y" violates not-null constraint
Hi everyone, I'm trying to create a userId column, fk to users.id but optional (so I don't set the notNull property): userId: char("user_id", { length: 12, }).references(() => users.id), But when I try to insert a value with NULL user_id, I get the error: null value in column "user_id" of relation "notifications" violates not-null constraint I've read that we're supposed to be able set null as fk if notNull is not present. I also tried with every combination of onDelete/onUpdate (set null, cascade etc.) but nothing works. Any idea?
1 replies
DTDrizzle Team
Created by pvman on 6/3/2024 in #help
Losing kysely types (using Kyselify) - getting [x: string]: any
Hello. I have some types problems using kysely with drizzle. I have the following config:
import { CamelCasePlugin, Kysely, PostgresDialect } from "kysely";
import { Kyselify } from "drizzle-orm/kysely";
import { createPool } from "#connectors/create-pool";
import {
posts,
} from "#schema";

export interface KyselyDatabase {
posts: Kyselify<typeof posts>;
}

export const db = new Kysely<KyselyDatabase>({
dialect: new PostgresDialect({
pool: createPool({
max: 1,
}),
}),
plugins: [new CamelCasePlugin()],
});
import { CamelCasePlugin, Kysely, PostgresDialect } from "kysely";
import { Kyselify } from "drizzle-orm/kysely";
import { createPool } from "#connectors/create-pool";
import {
posts,
} from "#schema";

export interface KyselyDatabase {
posts: Kyselify<typeof posts>;
}

export const db = new Kysely<KyselyDatabase>({
dialect: new PostgresDialect({
pool: createPool({
max: 1,
}),
}),
plugins: [new CamelCasePlugin()],
});
When I do the following query:
export const getPosts = async () => {
const posts = await db
.selectFrom("posts")
.select(["title", "id"])
.execute();
return posts ?? null;
};
export const getPosts = async () => {
const posts = await db
.selectFrom("posts")
.select(["title", "id"])
.execute();
return posts ?? null;
};
In vscode, I get the following type:
const getPosts: () => Promise<{
[x: string]: any;
}[]>
const getPosts: () => Promise<{
[x: string]: any;
}[]>
It doesn't infer the title and id types automatically so I have a bunch of type error in my code when using posts returned value. Am I doing something wrong or there is a problem with Kyselify?
1 replies
DTDrizzle Team
Created by pvman on 6/2/2024 in #help
Reusable pgTable column helper - losing type : [x: string] instead of user_id
Hi, I am trying to create a reusable column to use accross multiple tables. If I use the following :
export const userId = char("user_id", {
length: 10,
}).references(() => users.id);
export const userId = char("user_id", {
length: 10,
}).references(() => users.id);
I get the correct type in my KyselyDatabase interface. user_id: ColumnType<string, string, string>; I'd like it to be a function instead. But when I do the following :
export const userId = (name?: string) => {
return char(name ?? "user_id", {
length: 10,
}).references(() => users.id);
};
export const userId = (name?: string) => {
return char(name ?? "user_id", {
length: 10,
}).references(() => users.id);
};
I get the following type (thus breaking types accross all my db queries) [x: string]: ColumnType<string | null, string, string>;
6 replies