f2bear
f2bear
Explore posts from servers
DTDrizzle Team
Created by f2bear on 11/8/2023 in #help
Drizzle kit migration doesn't reflect schema's relations
I have two schemas in my application, one for parent courses and another one for child courses; they have a one-to-many relation between each other (one parent course can have multiple child courses). When I run npx drizzle-kit generate:pg I see the output in the CLI stating that non of them have foreign keys and when I check the snapshot I verified that no foreign key is present. My config looks like this:
// drizzle.config.ts
export default {
schema: './src/schemas/*.ts',
driver: 'pg',
out: './drizzle',
dbCredentials: {
connectionString: dbConnectionString,
},
} satisfies Config;
// drizzle.config.ts
export default {
schema: './src/schemas/*.ts',
driver: 'pg',
out: './drizzle',
dbCredentials: {
connectionString: dbConnectionString,
},
} satisfies Config;
And both my schemas and their declared relationships are in ./src/schemas/:
// parentCoursesSchema.ts
import { pgTable, text, varchar, integer } from 'drizzle-orm/pg-core';

const parentCourses = pgTable('parentCourses', {
id: integer('curso parent').primaryKey(),
titulo: varchar('titulo', { length: 255 }),
// rest of the schema
});

export default parentCourses;
// parentCoursesSchema.ts
import { pgTable, text, varchar, integer } from 'drizzle-orm/pg-core';

const parentCourses = pgTable('parentCourses', {
id: integer('curso parent').primaryKey(),
titulo: varchar('titulo', { length: 255 }),
// rest of the schema
});

export default parentCourses;
// coursesSchema.ts
import {
pgTable,
text,
varchar,
integer,
time,
timestamp,
boolean,
} from 'drizzle-orm/pg-core';

const courses = pgTable('courses', {
nombre: varchar('nombre', { length: 255 }).primaryKey(),
cursoParent: integer('curso parent'),
// rest of the schema
});

export default courses;
// coursesSchema.ts
import {
pgTable,
text,
varchar,
integer,
time,
timestamp,
boolean,
} from 'drizzle-orm/pg-core';

const courses = pgTable('courses', {
nombre: varchar('nombre', { length: 255 }).primaryKey(),
cursoParent: integer('curso parent'),
// rest of the schema
});

export default courses;
// coursesRelations.ts
import { relations } from 'drizzle-orm';
import courses from './coursesSchema';
import parentCourses from './parentCoursesSchema';

export const coursesRelations = relations(courses, ({ one }) => ({
parentCourse: one(parentCourses, {
fields: [courses.cursoParent],
references: [parentCourses.id],
}),
}));

export const parentCoursesRelations = relations(parentCourses, ({ many }) => ({
courses: many(courses),
}));
// coursesRelations.ts
import { relations } from 'drizzle-orm';
import courses from './coursesSchema';
import parentCourses from './parentCoursesSchema';

export const coursesRelations = relations(courses, ({ one }) => ({
parentCourse: one(parentCourses, {
fields: [courses.cursoParent],
references: [parentCourses.id],
}),
}));

export const parentCoursesRelations = relations(parentCourses, ({ many }) => ({
courses: many(courses),
}));
Does anyone knows what I'm doing wrong here?
7 replies
TTCTheo's Typesafe Cult
Created by f2bear on 2/6/2023 in #questions
Problems with useQuery.onSuccess()
Hi there, I'm doing a query on my trpc router and when it succeds I want to put that data on a useState that expects a ReactElement based upon the data from the query. The data from the query response is an array with objects like {id: string, title: string}, when I get them I run a data.forEach() on the returning array but when I have more than one object on the array only one is displayed on the client. Here is a snippet of what I'm doing, perhaps someone can see what's wrong with my code
const CategoriesIndex = ({ session }: Props) => {
const [links, setLinks] = useState<ReactElement<typeof CategoryLink>[]>([]);
api.menu.getSections.useQuery(undefined, {
enabled: session.user !== undefined,
onSuccess(data) {
console.log(data);
data.forEach((categorie) => {
setLinks([
...links,
<CategoryLink
title={categorie.title}
id={categorie.id}
key={categorie.id}
items={0}
/>,
]);
});
},
});
return (
<div className="col-start-1 col-end-7 row-start-3 grid w-full justify-items-center gap-4">
<Button
style="rounded-lg bg-gray-600 h-10 col-start-1 col-end-7 w-full"
name="add-category"
label="Añadir sección"
cb={mutation.mutate}
/>
{links}
</div>
);
const CategoriesIndex = ({ session }: Props) => {
const [links, setLinks] = useState<ReactElement<typeof CategoryLink>[]>([]);
api.menu.getSections.useQuery(undefined, {
enabled: session.user !== undefined,
onSuccess(data) {
console.log(data);
data.forEach((categorie) => {
setLinks([
...links,
<CategoryLink
title={categorie.title}
id={categorie.id}
key={categorie.id}
items={0}
/>,
]);
});
},
});
return (
<div className="col-start-1 col-end-7 row-start-3 grid w-full justify-items-center gap-4">
<Button
style="rounded-lg bg-gray-600 h-10 col-start-1 col-end-7 w-full"
name="add-category"
label="Añadir sección"
cb={mutation.mutate}
/>
{links}
</div>
);
18 replies