#Auto increment

3 messages · Page 1 of 1 (latest)

mint vault
#

How to autoincrement my DB id? I tried setting it to number and primarykey but when I don't pass in an id during insert, I get the error:
[ERROR] undefined cannot be passed as argument to the database.

So how do I use autoinc?

barren cometBOT
#

Hi @mint vault. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro DB does not currently support auto-incrementing IDs directly. When defining your table schema, you need to specify an id column with the primaryKey property, but you must provide a unique value for this id each time you insert a new row into the table.

Here's an example of how you might define a table with an id column:

const Author = defineTable({ 
  columns: { 
    id: column.number({ primaryKey: true }), 
    name: column.text(), 
  }
});

And here's how you might insert a new row into this table, providing a unique id:

await db.insert(Comment).values({ 
  id: uniqueId, 
  content: 'Hello, world!' 
});

In this example, uniqueId would need to be a unique number that you generate or keep track of in your application.

If you're seeing an error when you try to insert a row without providing an id, it's likely because the id column is a primary key and cannot be undefined.

For more information, you can refer to the Astro DB documentation (https://docs.astro.build/en/guides/astro-db#table-references).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: