In a transaction, how do you ignore generated field requirements?

export interface TextDocumentTable {
id: Generated<number>;
created_at: Generated<Date>;
}

export interface TextDocumentChunkTable {
id: Generated<number>;
text_document_id: number;
created_at: Generated<Date>;
text: string;
}

export type TextDocumentRow = Selectable<TextDocumentTable>;
export type TextDocumentChunkRow = Selectable<TextDocumentChunkTable>;
export interface TextDocumentTable {
id: Generated<number>;
created_at: Generated<Date>;
}

export interface TextDocumentChunkTable {
id: Generated<number>;
text_document_id: number;
created_at: Generated<Date>;
text: string;
}

export type TextDocumentRow = Selectable<TextDocumentTable>;
export type TextDocumentChunkRow = Selectable<TextDocumentChunkTable>;
await this.db.transaction().execute(async (trx) => {

// 2nd param is TextDocumentRow
const insertedTextDocument = await insertTextDocument(trx, document);

// 2nd param is TextDocumentChunkRow
await insertTextDocumentChunk(trx, {
text_document_id: insertedTextDocument.id,
text: document.text_content,
});
});
await this.db.transaction().execute(async (trx) => {

// 2nd param is TextDocumentRow
const insertedTextDocument = await insertTextDocument(trx, document);

// 2nd param is TextDocumentChunkRow
await insertTextDocumentChunk(trx, {
text_document_id: insertedTextDocument.id,
text: document.text_content,
});
});
Typescript will complain that insertTextDocumentChunk needs id and created_at even though they are Generated values.
Solution:
im assuming id/created_at are nullable on document
Jump to solution
5 Replies
Solution
ohmi
ohmi2y ago
im assuming id/created_at are nullable on document
ohmi
ohmi2y ago
while the generated schema is expecting them to not be null
Theo Gravity
Theo GravityOP2y ago
Ah. That's what I'm missing. Silly mistake Thank you
Theo Gravity
Theo GravityOP2y ago
Actually, I figured out what was really wrong - I shouldn't have had to add an optional on those fields. I was using Selectable<TextDocumentTable> and Selectable<TextDocumentChunkTable> for the insert inputs for insertTextDocument / insertTextDocumentChunk when it should be using Insertable instead of Selectable
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server