withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
44.07k stars 2.3k forks source link

Error in DB push #11229

Open javimata opened 3 weeks ago

javimata commented 3 weeks ago

Astro Info

Astro                    v4.7.0
Node                     v20.12.2
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/vercel/serverless
Integrations             @astrojs/react
                         @astrojs/tailwind
                         astro-auth
                         astro:db
                         @astrojs/db/file-url
                         astro-icon

If this issue only occurs in one browser, which browser is a problem?

cli

Describe the Bug

I have this schema but i receive a error

Database schema is out of date. Pushing database schema updates... https://db.services.astro.build/db/push failed: 400 Bad Request {"success":false,"error":{"code":"SQL_QUERY_FAILED","details":"SQLITE_CONSTRAINT: SQLite error: FOREIGN KEY constraint failed"}} /db/push fetch failed: 400 Bad Request

import { column, defineDb } from 'astro:db'

const Users = {
    columns: {
        userName: column.text(),
        userEmail: column.text({ primaryKey: true }),
        userImage: column.text({ optional: true }),
        createdAt: column.date({ default: new Date() }),
        lastVisit: column.date({ default: new Date() }),
        accountType: column.text({ default: 'free' }),
        accountProvider: column.text({ default: 'email' }),
        userPassword: column.text({ optional: true }),
        userStatus: column.number({ default: 1 }),
        credits: column.number({ default: 5 }),
        notes: column.text({ optional: true })
    }
}

const Recipes = {
    columns: {
        id: column.number({ primaryKey: true }),
        prompt: column.text(),
        tokens: column.text(),
        result: column.text(),
        user: column.text({ references: () => Users.columns.userEmail }),
        createdAt: column.date({ default: new Date() }),
        favorite: column.boolean({ default: false }),
        rating: column.number({ default: 0 }),
        status: column.number({ default: 1 }),
        modelVersion: column.text(),
        slug: column.text({ unique: true })
    }
}

const Settings = {
    columns: {
        id: column.number({ primaryKey: true }),
        user: column.text({ references: () => Users.columns.userEmail }),
        languageResponse: column.text({ default: 'spanish' }),
        measurement: column.text({ default: 'international' }),
        vegetarian: column.boolean({ default: false }),
        vegan: column.boolean({ default: false }),
        keto: column.boolean({ default: false }),
        sugarfree: column.boolean({ default: false }),
        glutenfree: column.boolean({ default: false }),
        notificationsEmail: column.boolean({ default: true }),
        notificationsPush: column.boolean({ default: true }),
        theme: column.text({ default: 'default' })
    }
}

export default defineDb({
    tables: {
        Users,
        Recipes,
        Settings
    }
})

What's the expected result?

a clean push to studio

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-rjvwlr

Participation

javimata commented 3 weeks ago

When the DB is empty this schema work fine, when the DB have info this crash

matthewp commented 3 weeks ago

Did you add or remove any columns when this happened?

javimata commented 3 weeks ago

Did you add or remove any columns when this happened?

I add a new column, i use --force-reset and my db is erased and add the new columns, but i need do --forcec-reset each that i need do a push, and i don't wanna erase my db info in each update

matthewp commented 3 weeks ago

Which is the new column you added that triggered this problem?

javimata commented 3 weeks ago

Which is the new column you added that triggered this problem?

The last one is un Users the column notes, now i'm adding 2 new tables and receive the same error, always need put --force-reset, this is not functional for me