janosch.hrm
janosch.hrm
DTDrizzle Team
Created by janosch.hrm on 11/3/2023 in #help
Unable to infer relation
I have the following tables and relations:
export const platforms = pgTable('platforms', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
});

export const platformsRelations = relations(platforms, ({ many }) => ({
founders: many(founders),
}));

export const founders = pgTable('founders', {
id: serial('id').primaryKey(),
platformId: integer('platform_id')
.references(() => platforms.id)
.notNull(),
name: text('name').notNull().unique(),
});
export const platforms = pgTable('platforms', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
});

export const platformsRelations = relations(platforms, ({ many }) => ({
founders: many(founders),
}));

export const founders = pgTable('founders', {
id: serial('id').primaryKey(),
platformId: integer('platform_id')
.references(() => platforms.id)
.notNull(),
name: text('name').notNull().unique(),
});
My goal is to get a single platform, and all the associated founders with this query:
export const getPlatformData = async (platformName: string) => {
const platformData = await db.query.platforms.findFirst({
where: eq(platforms.name, platformName),
with: {
founders: true,
},
});

return platformData;
};
export const getPlatformData = async (platformName: string) => {
const platformData = await db.query.platforms.findFirst({
where: eq(platforms.name, platformName),
with: {
founders: true,
},
});

return platformData;
};
But this fails with the error Internal error: Error: There is not enough information to infer relation "platforms.founders" . I'm not exactly sure what I can do to fix this.
4 replies
DTDrizzle Team
Created by janosch.hrm on 11/2/2023 in #help
.all() API not available
I have two tables that I want to join, and I want to join them to get the following result:
type Result = {
platform: Platform
founders Founder[]
}
type Result = {
platform: Platform
founders Founder[]
}
export const getPlatformData = async (platformName: string) => {
const { ...platformFields } = getTableColumns(platforms);

const platformData = await db
.select({
...platformFields,
founders: {
name: founders.name,
},
})
.from(platforms)
.innerJoin(founders, eq(founders.platformId, platforms.id))
.where(eq(platforms.name, platformName));

return platformData[0];
export const getPlatformData = async (platformName: string) => {
const { ...platformFields } = getTableColumns(platforms);

const platformData = await db
.select({
...platformFields,
founders: {
name: founders.name,
},
})
.from(platforms)
.innerJoin(founders, eq(founders.platformId, platforms.id))
.where(eq(platforms.name, platformName));

return platformData[0];
Right now, I only get back a single Founder with the join, so I wanted to use the .all() API, but it just didn't work. How would I do this?
2 replies
DTDrizzle Team
Created by janosch.hrm on 10/23/2023 in #help
error: column "x" is in a primary key
I'm getting the following error, when trying to push my schema to my remote db: error: column "x" is in a primary key The weird thing is that everything works fine, when I drop all tables and run it for the first time, but if I try to re-run drizzle-kit push:pg, this is the error I get, even if the schema has not changed at all. Does anyone know what might cause this?
{
length: 109,
severity: 'ERROR',
code: '42P16',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '7287',
routine: 'ATExecDropNotNull'
}
{
length: 109,
severity: 'ERROR',
code: '42P16',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '7287',
routine: 'ATExecDropNotNull'
}
1 replies