#how do i create a column that references another column from the same table?
8 messages · Page 1 of 1 (latest)
Astro DB supports referencing columns in other tables using the references property, but the knowledge sources do not provide an explicit example or guidance for self-referencing columns (i.e., a column referencing another column in the same table). The error you are seeing is a TypeScript limitation due to the circular reference when trying to access Category.columns.id within the same object definition.
The documentation suggests using the references property for foreign keys, but only shows examples referencing columns from different tables, not self-references. There is no mention of a workaround or supported pattern for self-referencing columns in the provided sources. Therefore, based on the current documentation, it is unclear if or how self-referencing columns are supported in Astro DB, and your approach may not be directly supported at this time Define your database Table configuration reference.
If you need this feature, you may want to follow up with the Astro team or check for updates in the official documentation.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@safe shoal Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books:
In your example:
const Sample = defineTable({
columns: {
id: column.number({ primaryKey: true }),
name: column.text(),
},
})
The type of the id column is number, as defined by column.number().
Astro DB automatically generates TypeScript types for your tables based on your schema. However, the documentation does not mention a specific type export for referencing the type of a column directly (such as SampleId or similar). The generated type for a row in your table will have id: number Define your database Table configuration reference.
If you need to reference the type of the id column elsewhere, you can use the number type directly. There is no mention in the knowledge sources of a utility type exported by Astro DB for this purpose.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@safe shoal Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. One moment! :books:
I'm sorry, but the knowledge sources do not provide explicit guidance or examples on how to give a type annotation to the function passed to the references property in Astro DB's defineTable API, especially for self-referencing columns. The documentation only shows usage like:
authorId: column.number({ references: () => Author.columns.id })
for referencing columns in other tables, without specifying a type for the references function Define your database Table configuration reference.