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

[BUG]: Drizzle Studio not showing some relation #2262

Closed
TheGrinch00 opened this issue May 5, 2024 · 2 comments
Closed

[BUG]: Drizzle Studio not showing some relation #2262

TheGrinch00 opened this issue May 5, 2024 · 2 comments
Assignees
Labels
bug Something isn't working drizzle-studio Issues related to Drizzle Studio

Comments

@TheGrinch00
Copy link

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.20.17

Describe the Bug

I have a MySQL schema with many to many relations and there are some that are not shown at all in Drizzle Studio, even if in TypeScript I can join them using the with parameter as shown here:

Screenshot 2024-05-05 at 18 34 23

This is a relations saying that a Product can have many ItemCategory and a single ItemCategory can be assigned to many Products.

In my schema, here is how I defined them:

export const ProductTable = mysqlTable("products", {
  id: int("id").primaryKey().autoincrement().notNull(),
  name: varchar("name", { length: 127 }).notNull(),
  disabled: tinyint("disabled").default(0).notNull(),
});

export const ProductTableRelations = relations(ProductTable, ({ many }) => ({
  productsMedias: many(ProductMediaTable),
  productsVisits: many(VisitProductTable),
  productsCategories: many(ProductCategoryTable),
}));

export const ItemCategoryTable = mysqlTable("item_categories", {
  id: int("id").primaryKey().autoincrement().notNull(),
  name: varchar("name", { length: 256 }).notNull(),
  disabled: tinyint("disabled").default(0).notNull(),
});

export const ItemCategoryTableRelations = relations(
  ItemCategoryTable,
  ({ many }) => ({
    productsCategories: many(ProductCategoryTable),
  }),
);

export const ProductCategoryTable = mysqlTable(
  "products_categories",
  {
    id: int("id").primaryKey().autoincrement().notNull(),
    productId: int("productId")
      .notNull()
      .references(() => ProductTable.id, {
        onDelete: "restrict",
        onUpdate: "cascade",
      }),
    categoryId: int("categoryId")
      .notNull()
      .references(() => ItemCategoryTable.id, {
        onDelete: "restrict",
        onUpdate: "cascade",
      }),
  },
  (table) => {
    return {
      productIdIdx: index("productId_idx").on(table.productId),
      categoryIdIdx: index("categoryId_idx").on(table.categoryId),
    };
  },
);

export const ProductCategoryTableRelations = relations(
  ProductCategoryTable,
  ({ one }) => ({
    product: one(ProductTable, {
      fields: [ProductCategoryTable.productId],
      references: [ProductTable.id],
    }),
    itemCategory: one(ItemCategoryTable, {
      fields: [ProductCategoryTable.categoryId],
      references: [ItemCategoryTable.id],
    }),
  }),
);

However, when I open the ProductCategoryTable on Drizzle Studio, I only see the relation to the products and not to the categories:

Screenshot 2024-05-05 at 18 39 45

Also, the ItemCategoryTable does not show relation to ProductCategoryTable as shown here:

Screenshot 2024-05-05 at 18 42 24

I already other many to many relations without any issue, but this one and some other just wont work

Expected behavior

I expect the relations to be shown in Drizzle Studio just like in the TS code

Environment & setup

I'm using Node 18.19.1

@TheGrinch00 TheGrinch00 added the bug Something isn't working label May 5, 2024
@realmikesolo realmikesolo added the drizzle-studio Issues related to Drizzle Studio label May 7, 2024
@RomanNabukhotnyi
Copy link
Contributor

@TheGrinch00 Hey! Try updating drizzle-kit to the latest version

@TheGrinch00
Copy link
Author

@RomanNabukhotnyi I updated it to version 0.21.1 and now it gave me some errors that, with previous versions, went unspotted.

After solving all the errors I can confirm that now all my relations are correctly picked up and shown in Drizzle Studio, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle-studio Issues related to Drizzle Studio
Projects
None yet
Development

No branches or pull requests

4 participants