medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.84k stars 2.58k forks source link

Query failed with linked model #9168

Closed darara47 closed 1 month ago

darara47 commented 1 month ago

Bug report

Describe the bug

When running a query that links the CustomerModule and BadgeModule in a custom Medusa module, an error occurs. The system generates a SQL query that targets a non-existent table due to incorrect capitalization in the table name. Specifically, it attempts to query "customer_customer_badgesmodule_badge", but the correct table is "customer_customer_badgesModule_badge" (note the capitalization of badgesModule).

System information

Medusa version (including plugins): 1.20.6-preview-20240917110030 Node.js version: v20.11.1 Database: PostgreSQL 15.2 Operating system: MacOS Sonoma Browser (if relevant):

Steps to reproduce the behavior

  1. Create custom module:
    export const BADGES_MODULE = "badgesModule";
  2. Define a link between modules:
    export default defineLink(
    {
    linkable: CustomerModule.linkable.customer,
    isList: true,
    },
    {
    linkable: BadgeModule.linkable.badge,
    isList: true,
    }
    );
  3. Run the following query:
    const { data: badges } = await query.graph({
    entity: "badge",
    fields: ["id", "name", "customers.*"],
    });
  4. An error occurs when the query is executed.

Expected behavior

The query should execute and return the requested data, including the linked model (badge with associated customers).

Screenshots

Zrzut ekranu 2024-09-17 o 15 13 54

Additional context

The issue appears to stem from the query generated by the system:

select "l0"."badge_id", "l0"."customer_id" 
from "public"."customer_customer_badgesmodule_badge" as "l0" 
where "l0"."deleted_at" is null and "l0"."badge_id" in ('1', '2', '3', '4', '5')

The query is targeting the table "customer_customer_badgesmodule_badge", which does not exist. The correct table name should be "customer_customer_badgesModule_badge" (note the capitalization of badgesModule).

Changing the table name manually using pgAdmin to "customer_customer_badgesmodule_badge" resolves the issue and the query returns the correct data.

riqwan commented 1 month ago

Hey @darara47,

We had a regression in one of our releases, which has already been fixed, can you upgrade to the latest preview version, run npx medusa migrations run && npx medusa links sync and try again?

adambarath commented 1 month ago

I had similar issue and npx medusa links sync helped me, thanks a lot!

Hey @darara47,

We had a regression in one of our releases, which has already been fixed, can you upgrade to the latest preview version, run npx medusa migrations run && npx medusa links sync and try again?

riqwan commented 1 month ago

Glad to hear that! 🎉