Foxtrot
Foxtrot
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
i think you can use 'and' operator.
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
@angelelz Thanks for your valuable time. I appreciate your help ❤️ .
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
it's working
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
export { addresses, addressesRelations } from "./addresses";
export { institutes } from "./institutes";
export { users, usersRelations } from "./users";
export { addresses, addressesRelations } from "./addresses";
export { institutes } from "./institutes";
export { users, usersRelations } from "./users";
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
i didnt export the relation schema
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
found the error.
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
@databse/schema
export { addresses } from "./addresses";
export { institutes } from "./institutes";
export { users } from "./users";
export { addresses } from "./addresses";
export { institutes } from "./institutes";
export { users } from "./users";
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
Yes,
import constant from "@/utils/constant";
import * as schemas from "@database/schema";
import "dotenv/config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

const client = postgres({
database: constant().DB_NAME,
host: constant().DB_HOST,
port: constant().DB_PORT,
password: constant().DB_PASSWORD,
user: constant().DB_USER,
});

export default drizzle(client, { schema: schemas });
import constant from "@/utils/constant";
import * as schemas from "@database/schema";
import "dotenv/config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

const client = postgres({
database: constant().DB_NAME,
host: constant().DB_HOST,
port: constant().DB_PORT,
password: constant().DB_PASSWORD,
user: constant().DB_USER,
});

export default drizzle(client, { schema: schemas });
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
If i go with this approach i am getting the following error: "Cannot read properties of undefined (reading 'referencedTable')" am i missing something that i didn't noticed? Schema is as above i mentioned earlier.
const user = await db.query.users.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: {
addresses: true,
},
});
const user = await db.query.users.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: {
addresses: true,
},
});
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
Thanks for your response. User Table:
export const users = pgTable("users", {
id: serial("id").primaryKey(),
...
instituteId: serial("institute_id")
.notNull()
.references(() => institutes.id)
});

export const usersRelations = relations(users, ({ many, one }) => ({
addresses: many(addresses),
institute: one(institutes, {
fields: [users.instituteId],
references: [institutes.id],
}),
}));
export const users = pgTable("users", {
id: serial("id").primaryKey(),
...
instituteId: serial("institute_id")
.notNull()
.references(() => institutes.id)
});

export const usersRelations = relations(users, ({ many, one }) => ({
addresses: many(addresses),
institute: one(institutes, {
fields: [users.instituteId],
references: [institutes.id],
}),
}));
Address Table:
export const addresses = pgTable("addresses", {
id: serial("id").primaryKey(),
type: addressTypeEnum("type").notNull(),
street: varchar("street", { length: 255 }).notNull(),
...
userId: serial("user_id").references(() => users.id)
});

export const addressesRelations = relations(addresses, ({ one }) => ({
user: one(users, {
fields: [addresses.userId],
references: [users.id],
}),
}));
export const addresses = pgTable("addresses", {
id: serial("id").primaryKey(),
type: addressTypeEnum("type").notNull(),
street: varchar("street", { length: 255 }).notNull(),
...
userId: serial("user_id").references(() => users.id)
});

export const addressesRelations = relations(addresses, ({ one }) => ({
user: one(users, {
fields: [addresses.userId],
references: [users.id],
}),
}));
Fetching data:
const user = await db
.select()
.from(users)
.innerJoin(addresses, eq(users.id, addresses.userId))
.where(eq(users.id, id));
const user = await db
.select()
.from(users)
.innerJoin(addresses, eq(users.id, addresses.userId))
.where(eq(users.id, id));
23 replies
DTDrizzle Team
Created by Mr.T 🐻⛓ on 9/16/2023 in #help
Trying to get relations from one table to another, as one-to-many but get only one value
same issue. 😦
23 replies