drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Different behavior on returning JSON field entity vs. included entity

Open tacomanator opened this issue 2 years ago • 0 comments

What version of drizzle-orm are you using?

0.26.1

What version of drizzle-kit are you using?

0.18.1

Describe the Bug

Schema:

import { relations } from "drizzle-orm";
import { sqliteTable, text, blob } from "drizzle-orm/sqlite-core";

type Address = { postcode: string; address: string };

export const Customer = sqliteTable("Customer", {
  id: text("id").primaryKey(),
  address: blob("address", { mode: "json" }).$type<Address>(),
});

export const Site = sqliteTable("Site", {
  id: text("id").primaryKey(),
  customerId: text("id"),
  address: blob("address", { mode: "json" }).$type<Address>(),
});

export const siteRelations = relations(Site, ({ one }) => ({
  customer: one(Customer, {
    fields: [Site.customerId],
    references: [Customer.id],
  }),
}));
  1. Create data in both tables
db.insert(Customer)
  .values({ id: "1", address: { postcode: "123", address: "123" } })
  .run();

db.insert(Site)
  .values({id: "1", customerId: "1", address: { postcode: "123", address: "123" } })
  .run();

Data will be created successfully.

  1. Query normally (both should succeed and address is json object)
db.select().from(Site); // [{ id: "1", customerId: "1", address: { postcode: "123", address: "123" }]
db.query.Site.findFirst({ columns: { id: true, customerId: true, address: true } }); // same as above
  1. Add the relation (address will be included as well):
db.query.Site.findFirst({
  columns: { address: true },
  with: { customer: { columns: { address: true } } },
})

Fails with - error SqliteError: JSON cannot hold BLOB values.

Expected behavior

Behavior of including address should be consistent.

Note: even though we're BLOB is not supposed to be used?, it's been working in absence of #485.

Environment & setup

No response

tacomanator avatar May 26 '23 09:05 tacomanator