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

Nuxt Drizzle: Named column causing Error: 'Key is undefined for item. Item was not written to cache.' #20

Closed
6 tasks done
Butch78 opened this issue Mar 17, 2025 · 11 comments
Labels

Comments

@Butch78
Copy link

Butch78 commented Mar 17, 2025

Describe the bug

Loving Rstore so far, I'm building a Job Board and I'm just running into a small issue with using the new drizzle features, I've create an Office schema:

import { nanoid } from "nanoid";
import { sqliteTable, text, integer, AnySQLiteColumn } from "drizzle-orm/sqlite-core";
import { companies } from "./companies";
import { timestamps } from "./timestamps";

export const offices = sqliteTable("offices", {
  id: text("id")
    .primaryKey()
    .$default(() => nanoid()),
  name: text("name").default(''),

  companyId: text("company_id")
    .notNull()
    .references((): AnySQLiteColumn => companies.id, { onDelete: "cascade" }),

  locationId: text("location_id").notNull(),

  ...timestamps,
});

When I do a simple query it will return the offices:

Image

It seems to not be reading the id?

Image

Help to send any further files that could help :)

Reproduction

I am using Nuxthub/CloudFlare D1 so it is quite difficult to create a replication

System Info

System:
    OS: Windows 11 10.0.27813
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 14.84 GB / 31.71 GB
  Binaries:
    Node: 23.9.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.9.2 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.15.1 - ~\AppData\Local\pnpm\pnpm.EXE
  Browsers:
    Edge: Chromium (134.0.3124.66)
    Internet Explorer: 11.0.27813.1000
  npmPackages:
    @rstore/nuxt-drizzle: ^0.5.5 => 0.5.5 
    @vitejs/plugin-vue: 5.2.2 => 5.2.2 
    nuxt: ^3.16.0 => 3.16.0 
    vue: ^3.5.13 => 3.5.13

Used Package Manager

pnpm

Validations

@Akryum
Copy link
Collaborator

Akryum commented Mar 17, 2025

Hello, could you try replicating your issue using a codesandbox, for example from this one? Thanks!
https://codesandbox.io/p/devbox/wonderful-sun-s4cgl6

@Butch78
Copy link
Author

Butch78 commented Mar 18, 2025

I'm trying but having no luck duplicating it, I'll keep trying to find a solution with my current project and maybe I can add a note to the documentation to mention what I missed

@Akryum
Copy link
Collaborator

Akryum commented Mar 18, 2025

Did you try putting the same drizzle schema and config?

@Butch78
Copy link
Author

Butch78 commented Mar 18, 2025

Here is a working example that is close to my repo:
https://codesandbox.io/p/devbox/polished-moon-7mg6t7?workspaceId=ws_SbYW1YcL9fFQRhqLzoCGLZ

I thought it might be because I have my schema in a folder like this:
server/database/schema/* with a matching drizzle config

But it still seems to detect all the schema:

Image

I'm so confused why it's able to detect all of the schema but not their primaryKeys:

Image

Is there a way to step through the rstore $rstore-drizzle-models.d.ts file creation?

@Akryum
Copy link
Collaborator

Akryum commented Mar 19, 2025

You can try adding a breakpoint in the node_modules/@rstore/nuxt-drizzle/dist/module.mjs file in the getModelsFormDrizzleSchema function: https://unpkg.com/browse/@rstore/[email protected]/dist/module.mjs

Then run nuxt with nuxi dev --inspect

@Butch78
Copy link
Author

Butch78 commented Mar 19, 2025

I was able to replicate the issue when you add rstore to an existing project:
https://github.com/Butch78/habit-test

Image

From what I could find while debugging: getSqliteTableConfig seems to not recognize existing primaryKeys()

Here it accepts the books primaryKey (created after rstore-drizzle was installed) but not habits, I think it is because the keyAsName is false in the habits table but is true in the books table

Image

@Butch78
Copy link
Author

Butch78 commented Mar 19, 2025

I created a pull request that solves the problem locally: #21

I think it still might need some further testing to best handling identifing the primaryKeys.

Thank you for you patience and I'm really excited to use rstore on my projects :)

@Akryum
Copy link
Collaborator

Akryum commented Mar 19, 2025

Could you try removing the column name here:

Image

@Butch78
Copy link
Author

Butch78 commented Mar 23, 2025

Sorry for the slow reply! I was moving :)

I tried it and it worked no issues:

Image

Image

@Akryum
Copy link
Collaborator

Akryum commented Mar 24, 2025

Okay so it looks like it's an issue with named columns

@Akryum Akryum changed the title Nuxt Drizzle: Key is undefined for item. Item was not written to cache. Nuxt Drizzle: Named column causing Error: 'Key is undefined for item. Item was not written to cache.' Mar 24, 2025
@Butch78
Copy link
Author

Butch78 commented Mar 24, 2025

Yeah removing the name column values e.g:

id: text("id")
    .primaryKey()
    .$default(() => nanoid())

===>

id: text()
    .primaryKey()
    .$default(() => nanoid())

Name columns is causing the issue!

@Akryum Akryum closed this as completed in bfe00c3 Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants