Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Data Insertion Type Mismatch in Astro DB Table Definitions #7792

Closed
suzulang opened this issue Apr 7, 2024 · 1 comment
Closed

Fixing Data Insertion Type Mismatch in Astro DB Table Definitions #7792

suzulang opened this issue Apr 7, 2024 · 1 comment
Labels
improve documentation Enhance existing documentation (e.g. add an example, improve description)

Comments

@suzulang
Copy link

suzulang commented Apr 7, 2024

📚 Subject area/topic

astro-db

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/guides/astro-db/

📋 Description of content that is out-of-date or incorrect

I will try to follow the documentation and configure astro-db.

The field names of the table provided in the documentation do not match the field names of the mock data in seed.ts

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

Title:
Issues with Inserting Data into Astro DB using astro:db

Body:

Hello,

I'm working with Astro DB and encountered an issue while trying to insert data into a table. Below are the details of my setup and the problem I've encountered.

Setup
Database Configuration (db/config.ts):

// db/config.ts
import { defineDb, defineTable, column } from 'astro:db';

const Comment = defineTable({
  columns: {
    author: column.text(),
    body: column.text(),
  }
})

export default defineDb({
  tables: { Comment },
})
Data Seeding Script (db/seed.ts):
// db/seed.ts
import { db, Comment } from 'astro:db';

export default async function() {
  await db.insert(Comment).values([
    { authorId: 1, body: 'Hope you like Astro DB!' },
    { authorId: 2, body: 'Enjoy!'},
  ])
}

When I run this setup, it produces the following output (as seen in the attached image):

image

Issue
The compiler throws an error regarding the authorId field, which does not match the defined structure in Comment table where the field is named author.

Solution Attempt
I modified the database configuration and the seeding script as follows:

Modified Database Configuration in db/config.ts:

Changed author: column.text(), to author: column.number(),

Modified Data Seeding Script (db/seed.ts):

// db/seed.ts
import { db, Comment } from 'astro:db';

export default async function() {
  await db.insert(Comment).values([
    { author: 1, body: 'Hope you like Astro DB!' },
    { author: 2, body: 'Enjoy!'},
  ])
}

After making these changes, the script runs successfully, as shown in the attached image:
image

Questions
Is there a recommended approach for debugging such type-related issues in Astro DB?
Are there best practices for structuring database configurations and seeding scripts to avoid these kinds of issues?
Thank you for your assistance!

@suzulang suzulang added the improve documentation Enhance existing documentation (e.g. add an example, improve description) label Apr 7, 2024
@sarah11918
Copy link
Member

Hi @suzulang!

As you're aware, Astro DB is a new and developing feature. I can see that you have questions about best practices, and we do not yet have established best practices for debugging. I am going to refer you to asking support questions in our Discord where some of our maintainers and community members may have suggestions as to how they debug, and structure their configurations.

You may also be interested in community resources that we have collected with various content and guides people have created to demonstrate their own process and usage of the feature. While we are still attempting to create comprehensive documentation, we are happy to showcase community content! There may be some content here that is useful or interesting: https://astro.build/blog/whats-new-march-2024/#showcase

I noted that you expressed interest in contributing to the docs in your earlier PR, and one way that helps us is when people create their own content, guides, articles and videos and then we do link to them from docs, for example: https://docs.astro.build/en/community-resources/content/#astro-db

If you create content of your own, please do submit a PR to this page and we will link to it! I note that we ourselves do not yet have a "top to bottom" continuous example, so these guides people write to demonstrate an actual blog feature they have built are always appreciated, and we love to showcase community content!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

No branches or pull requests

2 participants