johncmacy
johncmacy
Explore posts from servers
DTDrizzle Team
Created by johncmacy on 4/14/2025 in #help
Query to include parent with child infers types as `never`
Hello, In my schema, I have a self-referential foreign key set up:
export const Account = pgTable(
"account",
{
id: uuid("id").primaryKey().notNull().defaultRandom(),
parentId: uuid("parent_id"),
name: text().notNull(),
},
(table) => [
foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "account_parent_id_fkey",
}).onDelete("restrict"),
],
)
export const Account = pgTable(
"account",
{
id: uuid("id").primaryKey().notNull().defaultRandom(),
parentId: uuid("parent_id"),
name: text().notNull(),
},
(table) => [
foreignKey({
columns: [table.parentId],
foreignColumns: [table.id],
name: "account_parent_id_fkey",
}).onDelete("restrict"),
],
)
I'm trying to select the parent with the child like this:
const ParentAccount = aliasedTable(Account, "parent")

const accounts = (
await db
.select()
.from(Account)
.leftJoin(ParentAccount, eq(ParentAccount.id, Account.parentId))
)
const ParentAccount = aliasedTable(Account, "parent")

const accounts = (
await db
.select()
.from(Account)
.leftJoin(ParentAccount, eq(ParentAccount.id, Account.parentId))
)
However, the inferred type of accounts is:
{
account: never;
}[]
{
account: never;
}[]
1 replies
XXata
Created by johncmacy on 9/25/2024 in #help
Error: Option apiKey is required in Remix loader
Hello, I've been trying to query from Xata in a Remix loader, and am getting this error.
Error: Option apiKey is required
at XataClient.parseOptions_fn
Error: Option apiKey is required
at XataClient.parseOptions_fn
I'm aware that queries should only be run on the server, but from what I understand, Remix loaders only run on the server, so I don't think that's the issue.
export async function loader() {
const xata = getXataClient()
const topics = await xata.db.topics.getAll()
return json({ topics })
}
export async function loader() {
const xata = getXataClient()
const topics = await xata.db.topics.getAll()
return json({ topics })
}
I found a workaround, which is to manually add apiKey and branch to the XataClient instance that is generated by the CLI.
export const getXataClient = () => {
if (instance) return instance

// change this
instance = new XataClient()
// to this
instance = new XataClient({
apiKey: process.env.XATA_API_KEY,
branch: process.env.XATA_BRANCH,
})

return instance
}
export const getXataClient = () => {
if (instance) return instance

// change this
instance = new XataClient()
// to this
instance = new XataClient({
apiKey: process.env.XATA_API_KEY,
branch: process.env.XATA_BRANCH,
})

return instance
}
But that means I have to manually do that every time I update the schema. Is there something obvious I'm missing? Thanks!
5 replies