juan
juan
DTDrizzle Team
Created by juan on 12/12/2024 in #help
Too many nested objs on many-to-many relations
I get an object like
[
{
"id": "43119ea8-a293-4c89-aaba-4d699f89e34f",
"model": "3",
"brand": "Firulo",
"manufacturer": "Carlitos",
"homologations": [
{
"homologation": {
"id": "8fff866b-055a-406e-9c61-9e842c5a0a2e",
"name": "una homologación cualquiera",
"countries": [
{
"country": {
"id": "ar",
"name": "Argentina"
}
},
{
"country": {
"id": "us",
"name": "United States"
}
}
]
}
}
]
},
{
"id": "4a386002-274b-4e15-8ed9-7ac3aaabe178",
"model": "Coso®",
"brand": "El blanco",
"manufacturer": "Juan",
"homologations": [
{
"homologation": {
"id": "8fff866b-055a-406e-9c61-9e842c5a0a2e",
"name": "una homologación cualquiera",
"countries": [
{
"country": {
"id": "ar",
"name": "Argentina"
}
},
{
"country": {
"id": "us",
"name": "United States"
}
}
]
}
}
]
}
]
[
{
"id": "43119ea8-a293-4c89-aaba-4d699f89e34f",
"model": "3",
"brand": "Firulo",
"manufacturer": "Carlitos",
"homologations": [
{
"homologation": {
"id": "8fff866b-055a-406e-9c61-9e842c5a0a2e",
"name": "una homologación cualquiera",
"countries": [
{
"country": {
"id": "ar",
"name": "Argentina"
}
},
{
"country": {
"id": "us",
"name": "United States"
}
}
]
}
}
]
},
{
"id": "4a386002-274b-4e15-8ed9-7ac3aaabe178",
"model": "Coso®",
"brand": "El blanco",
"manufacturer": "Juan",
"homologations": [
{
"homologation": {
"id": "8fff866b-055a-406e-9c61-9e842c5a0a2e",
"name": "una homologación cualquiera",
"countries": [
{
"country": {
"id": "ar",
"name": "Argentina"
}
},
{
"country": {
"id": "us",
"name": "United States"
}
}
]
}
}
]
}
]
Would it be possible to remove the keys homologations, countries, etc? query https://pastebin.com/XWF0C1aY
2 replies
DTDrizzle Team
Created by juan on 12/3/2024 in #help
All other relations drop when creating a new one
export const homologationSchemas = createTable(
"homologation_schemas",
{
id: serialUuid(),
schema: jsonb().$type<AnyStateSchema>().notNull(),
name: varchar().notNull(),
description: text()
},
(hs) => ({
schema_index: index("schema_idx").on(hs.schema),
})
);

export const homologations = createTable(
"homologations",
{
id: serialUuid(),
name: varchar().notNull(),
productId: uuid("product_id"),
stateSchemaId: uuid("state_schema_id").notNull(),
},
(h) => ({
state_index: index("state_idx").on(h.state),
})
);

export const products = createTable(
"products",
{
id: serialUuid(),

name: varchar().notNull(),
}
)


export const homologationSchemaRelation = relations(homologations, ({ one }) => ({
stateSchema: one(homologationSchemas, {
fields: [homologations.stateSchemaId],
references: [homologationSchemas.id]
}),
}),
}))

// When I created this relation, the relation above just stops working (vscode doesn't show the property `product`, but it returns the data)
export const homologationProductRelation = relations(homologations, ({ one }) => ({
product: one(products, {
fields: [homologations.productId],
references: [products.id],
relationName: "relationname"
})
}))

// idk
export const productHomologationsRelation = relations(products, ({ many }) => ({
homologations: many(homologations, { relationName: "relationname" })
}))
export const homologationSchemas = createTable(
"homologation_schemas",
{
id: serialUuid(),
schema: jsonb().$type<AnyStateSchema>().notNull(),
name: varchar().notNull(),
description: text()
},
(hs) => ({
schema_index: index("schema_idx").on(hs.schema),
})
);

export const homologations = createTable(
"homologations",
{
id: serialUuid(),
name: varchar().notNull(),
productId: uuid("product_id"),
stateSchemaId: uuid("state_schema_id").notNull(),
},
(h) => ({
state_index: index("state_idx").on(h.state),
})
);

export const products = createTable(
"products",
{
id: serialUuid(),

name: varchar().notNull(),
}
)


export const homologationSchemaRelation = relations(homologations, ({ one }) => ({
stateSchema: one(homologationSchemas, {
fields: [homologations.stateSchemaId],
references: [homologationSchemas.id]
}),
}),
}))

// When I created this relation, the relation above just stops working (vscode doesn't show the property `product`, but it returns the data)
export const homologationProductRelation = relations(homologations, ({ one }) => ({
product: one(products, {
fields: [homologations.productId],
references: [products.id],
relationName: "relationname"
})
}))

// idk
export const productHomologationsRelation = relations(products, ({ many }) => ({
homologations: many(homologations, { relationName: "relationname" })
}))
5 replies
DTDrizzle Team
Created by juan on 11/5/2024 in #help
Self-referencing many to many relation
I've been trying to point some interfaces to similar interfaces using the following code:
export const interfaces = createTable(
"interfaces",
{
id: varchar().notNull().primaryKey(),
name: varchar().notNull(),
type: interfacesTypesEnum().notNull(),
frequency: varchar().notNull(),
}
)

export const interfacesInterfaces = createTable(
"similar_interfaces",
{
interfaceId: varchar("interface_id")
.notNull()
.references(() => interfaces.id),
similarId: varchar("similar_id")
.notNull()
.references(() => interfaces.id),
},
(t) => ({
pk: primaryKey({ columns: [t.interfaceId, t.similarId] }),
})
);

export const interfacesInterfacesRelation = relations(interfacesInterfaces, ({ one }) => ({
asd: one(interfaces, {
fields: [interfacesInterfaces.interfaceId],
references: [interfaces.id],
relationName: "1"
}),
similar: one(interfaces, {
fields: [interfacesInterfaces.similarId],
references: [interfaces.id],
relationName: "2"
})
}))

export const interfaceInterfacesRelation = relations(interfaces, ({ many }) => ({
similarInterfaces: many(interfacesInterfaces, { relationName: "1" }),
}));
export const interfaces = createTable(
"interfaces",
{
id: varchar().notNull().primaryKey(),
name: varchar().notNull(),
type: interfacesTypesEnum().notNull(),
frequency: varchar().notNull(),
}
)

export const interfacesInterfaces = createTable(
"similar_interfaces",
{
interfaceId: varchar("interface_id")
.notNull()
.references(() => interfaces.id),
similarId: varchar("similar_id")
.notNull()
.references(() => interfaces.id),
},
(t) => ({
pk: primaryKey({ columns: [t.interfaceId, t.similarId] }),
})
);

export const interfacesInterfacesRelation = relations(interfacesInterfaces, ({ one }) => ({
asd: one(interfaces, {
fields: [interfacesInterfaces.interfaceId],
references: [interfaces.id],
relationName: "1"
}),
similar: one(interfaces, {
fields: [interfacesInterfaces.similarId],
references: [interfaces.id],
relationName: "2"
})
}))

export const interfaceInterfacesRelation = relations(interfaces, ({ many }) => ({
similarInterfaces: many(interfacesInterfaces, { relationName: "1" }),
}));
But I can't get the relation property to show up on the querying side
3 replies