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

Unable to create indexes on astro db #10451

Closed
1 task
arch-fan opened this issue Mar 15, 2024 · 17 comments · Fixed by #10506
Closed
1 task

Unable to create indexes on astro db #10451

arch-fan opened this issue Mar 15, 2024 · 17 comments · Fixed by #10506
Assignees
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) pkg: db

Comments

@arch-fan
Copy link

Astro Info

Astro                    v4.5.4
Node                     v20.10.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             astro:db
                         @astrojs/db/file-url

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

No response

Describe the Bug

Astro db push fails at creating the indexes. It doesn't matter the way I name the index, it throws the same error:

✔ Connected to remote database.
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_UNKNOWN: SQLite error: index id_idx already exists"}}
/db/push fetch failed: 400 Bad Request

What's the expected result?

Indexes being created

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-deerzq?file=db%2Fconfig.ts

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Mar 15, 2024
@Princesseuh
Copy link
Member

cc @bholmesdev not sure if this is on the Studio or the DB side?

@arch-fan
Copy link
Author

It's on the studio

@matthewp matthewp added the - P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) label Mar 15, 2024
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Mar 15, 2024
@matthewp matthewp added - P4: important Violate documented behavior or significantly impacts performance (priority) and removed - P5: urgent Fix build-breaking bugs affecting most users, should be released ASAP (priority) labels Mar 15, 2024
@matthewp matthewp self-assigned this Mar 15, 2024
@matthewp
Copy link
Contributor

@arch-fan Did this happen the first time you pushed or had you previously pushed and made some schema changes and then pushed again?

@arch-fan
Copy link
Author

It was the first time I pushed to studio

@matthewp
Copy link
Contributor

matthewp commented Mar 15, 2024

Can you try to push again and let me know if you still get this error? Also, if you do, what version of astro and @astrojs/db?

@arch-fan
Copy link
Author

Still getting the same error:

✔ Connected to remote database.
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_UNKNOWN: SQLite error: index id_idx already exists"}}
/db/push fetch failed: 400 Bad Request

Sure, these are the versions:

dependencies:
@astrojs/db 0.8.5
astro 4.5.4

@arch-fan
Copy link
Author

Here you have the whole definition of the tables:

import { defineDb, defineTable, column, NOW } from "astro:db";

const Categories = defineTable({
  columns: {
    id: column.number({ primaryKey: true }),
    name: column.text({ unique: true }),
    slug: column.text({ unique: true }),
  },
  indexes: {
    slug_idx: {
      on: "slug",
      unique: true,
    },
    id_idx: {
      on: "id",
      unique: true,
    },
  },
});

const Authors = defineTable({
  columns: {
    id: column.number({ primaryKey: true }),
    name: column.text(),
  },
  indexes: {
    id_idx: {
      on: "id",
      unique: true,
    },
  },
});

const Posts = defineTable({
  columns: {
    id: column.number({ primaryKey: true }),
    title: column.text({ unique: true }),
    excerpt: column.text(),
    slug: column.text({ unique: true }),
    content: column.text(),
    date: column.date({ default: NOW }),
    categoryId: column.number({ references: () => Categories.columns.id }),
    authorId: column.number({ references: () => Authors.columns.id }),
  },
  indexes: {
    slug_idx: {
      on: "slug",
      unique: true,
    },
    title_excerpt_idx: {
      on: ["title", "excerpt"],
    },
  },
});

// https://astro.build/db/config
export default defineDb({
  tables: { Categories, Authors, Posts },
});

@matthewp
Copy link
Contributor

Can you try with @astrojs/db@0.8.6?

@arch-fan
Copy link
Author

Same

✔ Connected to remote database.
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_UNKNOWN: SQLite error: index id_idx already exists"}}
/db/push fetch failed: 400 Bad Request
dependencies:
@astrojs/db 0.8.6
astro 4.5.4

@matthewp
Copy link
Contributor

Ok, thank you, this is helpful. To clarify, you have tried the example you provided and get the problem there?

@arch-fan
Copy link
Author

Yep, is the same example. It also happens if I change the indexes names:

import { defineDb, defineTable, column, NOW } from "astro:db";

const Categories = defineTable({
  columns: {
    id: column.number({ primaryKey: true }),
    name: column.text({ unique: true }),
    slug: column.text({ unique: true }),
  },
  indexes: {
    slug2_idx: {
      on: "slug",
      unique: true,
    },
    id2_idx: {
      on: "id",
      unique: true,
    },
  },
});

const Authors = defineTable({
  columns: {
    id: column.number({ primaryKey: true }),
    name: column.text(),
  },
  indexes: {
    id2_idx: {
      on: "id",
      unique: true,
    },
  },
});

const Posts = defineTable({
  columns: {
    id: column.number({ primaryKey: true }),
    title: column.text({ unique: true }),
    excerpt: column.text(),
    slug: column.text({ unique: true }),
    content: column.text(),
    date: column.date({ default: NOW }),
    categoryId: column.number({ references: () => Categories.columns.id }),
    authorId: column.number({ references: () => Authors.columns.id }),
  },
  indexes: {
    slug2_idx: {
      on: "slug",
      unique: true,
    },
    title_excerpt2_idx: {
      on: ["title", "excerpt"],
    },
  },
});

// https://astro.build/db/config
export default defineDb({
  tables: { Categories, Authors, Posts },
});

Error:

✔ Connected to remote database.
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_UNKNOWN: SQLite error: index id2_idx already exists"}}
/db/push fetch failed: 400 Bad Request

@matthewp
Copy link
Contributor

@arch-fan That's not the same as the example, the example on has a Categories table. I just want to confirm, you can see this with the example and only the example? No extra code at all?

@matthewp
Copy link
Contributor

I get an error after adding a second table.

@matthewp
Copy link
Contributor

@arch-fan I think I know what's going on here. Indices names in SQLite are global to the database. So it looks like you have 2 indices with the same name, which is why it's producing this error. If you instead name them something like category_id2_idx and author_id2_idx then it should work.

We should probably detect this and provide a better error message for you.

@arch-fan
Copy link
Author

That seemed to be the problem, thank you!

By the way, Is this behavior expected?

juan@P7BZZXXD:~/code/node/blog$ pnpm astro db push --force-reset

> blog@0.0.1 astro /home/juan/code/node/blog
> astro "db" "push" "--force-reset"

✔ Connected to remote database.
Database schema is out of date.
Force-pushing to the database. All existing data will be erased.
Pushing database schema updates...
https://db.services.astro.build/db/push failed: 400 Bad Request
{"success":false,"error":{"code":"SQL_QUERY_FAILED","details":"SQL_INPUT_ERROR: SQL input error: table \"Categories\" already exists (at offset 13)"}}
/db/push fetch failed: 400 Bad Request

I had to delete them manually to be able to push them.

@matthewp
Copy link
Contributor

I don't think you should get that error, so let me treat that as a separate bug and I'll investigate.


For the indexing problem, I think we're going to fix with an API change, likely moving indexes out of the defineTable and into defineDb. This should solve the problem of duplicate index names.

@arch-fan
Copy link
Author

Also, when I delete tables from Astro Studio's console (didn't try with astro db cli) the tables don't update on the studio, but the db gets modified

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) pkg: db
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants