francis
francis
Explore posts from servers
DTDrizzle Team
Created by francis on 9/20/2024 in #help
How do you define two tables that have foreign keys to each other? drizzle-kit introspect fails
created issue for mutual foreign keys: https://github.com/drizzle-team/drizzle-orm/issues/2993 created issue for introspect reversing column order: https://github.com/drizzle-team/drizzle-kit-mirror/issues/574
5 replies
DTDrizzle Team
Created by francis on 9/20/2024 in #help
How do you define two tables that have foreign keys to each other? drizzle-kit introspect fails
note that: - this doesn't compile due to the mutual references causing type errors in inference - in addition, the generated testKey on child has the columns in inverse order
5 replies
DTDrizzle Team
Created by francis on 9/20/2024 in #help
How do you define two tables that have foreign keys to each other? drizzle-kit introspect fails
generated schema that fails:
import { sql } from "drizzle-orm";
import { foreignKey, pgSchema, pgTable, unique, uuid, type AnyPgColumn } from "drizzle-orm/pg-core";

export const drizzleTest = pgSchema("drizzle_test");

export const childInDrizzleTest = drizzleTest.table(
"child",
{
id: uuid("id")
.default(sql`uuid_generate_v4()`)
.primaryKey()
.notNull(),
otherId: uuid("other_id").notNull(),
},
(table) => {
return {
testKey: foreignKey({
columns: [table.id, table.otherId],
foreignColumns: [parentInDrizzleTest.otherId, parentInDrizzleTest.childId],
name: "test_key",
}).onDelete("cascade"),
};
},
);

export const parentInDrizzleTest = drizzleTest.table(
"parent",
{
id: uuid("id")
.default(sql`uuid_generate_v4()`)
.primaryKey()
.notNull(),
otherId: uuid("other_id").notNull(),
childId: uuid("child_id"),
},
(table) => {
return {
parentChildIdFkey: foreignKey({
columns: [table.childId],
foreignColumns: [childInDrizzleTest.id],
name: "parent_child_id_fkey",
}).onDelete("restrict"),
parentOtherIdChildIdKey: unique("parent_other_id_child_id_key").on(
table.otherId,
table.childId,
),
parentChildIdKey: unique("parent_child_id_key").on(table.childId),
};
},
);
import { sql } from "drizzle-orm";
import { foreignKey, pgSchema, pgTable, unique, uuid, type AnyPgColumn } from "drizzle-orm/pg-core";

export const drizzleTest = pgSchema("drizzle_test");

export const childInDrizzleTest = drizzleTest.table(
"child",
{
id: uuid("id")
.default(sql`uuid_generate_v4()`)
.primaryKey()
.notNull(),
otherId: uuid("other_id").notNull(),
},
(table) => {
return {
testKey: foreignKey({
columns: [table.id, table.otherId],
foreignColumns: [parentInDrizzleTest.otherId, parentInDrizzleTest.childId],
name: "test_key",
}).onDelete("cascade"),
};
},
);

export const parentInDrizzleTest = drizzleTest.table(
"parent",
{
id: uuid("id")
.default(sql`uuid_generate_v4()`)
.primaryKey()
.notNull(),
otherId: uuid("other_id").notNull(),
childId: uuid("child_id"),
},
(table) => {
return {
parentChildIdFkey: foreignKey({
columns: [table.childId],
foreignColumns: [childInDrizzleTest.id],
name: "parent_child_id_fkey",
}).onDelete("restrict"),
parentOtherIdChildIdKey: unique("parent_other_id_child_id_key").on(
table.otherId,
table.childId,
),
parentChildIdKey: unique("parent_child_id_key").on(table.childId),
};
},
);
5 replies
DTDrizzle Team
Created by francis on 9/20/2024 in #help
How do you define two tables that have foreign keys to each other? drizzle-kit introspect fails
sql source to generate tables:
create schema drizzle_test;

create table drizzle_test.child (
id uuid primary key default uuid_generate_v4(),
other_id uuid not null
);

create table drizzle_test.parent (
id uuid primary key default uuid_generate_v4(),
other_id uuid not null,
child_id uuid unique references drizzle_test.child (id) on delete restrict,
unique (other_id, child_id)
);

alter table drizzle_test.child add constraint test_key
foreign key (other_id, id)
references drizzle_test.parent (other_id, child_id)
on delete cascade deferrable initially deferred;
create schema drizzle_test;

create table drizzle_test.child (
id uuid primary key default uuid_generate_v4(),
other_id uuid not null
);

create table drizzle_test.parent (
id uuid primary key default uuid_generate_v4(),
other_id uuid not null,
child_id uuid unique references drizzle_test.child (id) on delete restrict,
unique (other_id, child_id)
);

alter table drizzle_test.child add constraint test_key
foreign key (other_id, id)
references drizzle_test.parent (other_id, child_id)
on delete cascade deferrable initially deferred;
5 replies
DTDrizzle Team
Created by terryball on 3/14/2024 in #help
Type error working with Postgres dates in v.0.30.1
this has been resolved for ages now I think
9 replies
DTDrizzle Team
Created by Mosch on 8/12/2024 in #help
defaultFn when using magic sql is not applying
check your table DDL to see if you have a default defined
14 replies
DTDrizzle Team
Created by Mosch on 8/12/2024 in #help
defaultFn when using magic sql is not applying
that would be why
14 replies
DTDrizzle Team
Created by Mosch on 8/12/2024 in #help
defaultFn when using magic sql is not applying
oh, do you not have a default defined at the sql level
14 replies
DTDrizzle Team
Created by CrashChicken on 8/13/2024 in #help
Using default values removes columns from insert and update types
in general, turn strict mode on
14 replies
DTDrizzle Team
Created by CrashChicken on 8/13/2024 in #help
Using default values removes columns from insert and update types
ah yep that'll do it
14 replies
DTDrizzle Team
Created by CrashChicken on 8/13/2024 in #help
Using default values removes columns from insert and update types
and what is the type generated by typeof testSchema.$inferSelect and typeof testSchema.$inferInsert
14 replies
DTDrizzle Team
Created by CrashChicken on 8/13/2024 in #help
Using default values removes columns from insert and update types
interesting. what driver?
14 replies
DTDrizzle Team
Created by Mosch on 8/12/2024 in #help
defaultFn when using magic sql is not applying
not sure what sql flavor you are using but at least for postgres, you have to explicitly list the columns you are inserting into if you want to skip some and have it filled by default: "The target column names can be listed in any order. If no list of column names is given at all, the default is all the columns of the table in their declared order; or the first N column names, if there are only N columns supplied by the VALUES clause or query. The values supplied by the VALUES clause or query are associated with the explicit or implicit column list left-to-right."
14 replies
DTDrizzle Team
Created by Chris Soul on 8/9/2024 in #help
Does introspect only generate schema in the "out" (./drizzle) directory?
I recommend setting that sort of thing up since it's relatively easy to do and have it be consistent
14 replies
DTDrizzle Team
Created by Chris Soul on 8/9/2024 in #help
Does introspect only generate schema in the "out" (./drizzle) directory?
postprocess-pulled-schema is an intermediate step I put in that runs sed over the output to change some things - e.g. introspect always pulls timestamp columns in 'string' mode, so I have "shx sed -i \"s/mode: 'string'/mode: 'date'/\" drizzle/schema.ts", to fix those
14 replies
DTDrizzle Team
Created by Chris Soul on 8/9/2024 in #help
Does introspect only generate schema in the "out" (./drizzle) directory?
(in a package.json script)
14 replies
DTDrizzle Team
Created by Chris Soul on 8/9/2024 in #help
Does introspect only generate schema in the "out" (./drizzle) directory?
"drizzle-kit introspect && npm run postprocess-pulled-schema && mv drizzle/schema.ts src/lib/db/schema.ts",
14 replies
DTDrizzle Team
Created by Chris Soul on 8/9/2024 in #help
Does introspect only generate schema in the "out" (./drizzle) directory?
here's my commands, fyi
14 replies
DTDrizzle Team
Created by Chris Soul on 8/9/2024 in #help
Does introspect only generate schema in the "out" (./drizzle) directory?
it's always been that way for me, I have a package script to generate the schema and copy it to the target
14 replies
DTDrizzle Team
Created by Jaymal on 4/4/2024 in #help
Timestamp formatted differently if fetched as relation rather than directly
tl;dr is that the relational query system casts the rows to json, and postgres to_json on a timestamp converts it to ISO, where a plain select returns it in sql timestamp format
19 replies