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 reference static cube names (inside schema folder) in the dynamic schema while creating joins #1880

Open
manickaws opened this issue Jan 26, 2021 · 5 comments
Assignees
Labels
data modeling question The issue is a question. Please use Stack Overflow for questions.

Comments

@manickaws
Copy link

manickaws commented Jan 26, 2021

Description:
I have static cubes inside the schema folder (eg: Users). I also create dynamic schema fetching from database. The dynamic schema works perfectly. However when i try to join the dynamic cubes and the static cubes , i get reference error "Users is not defined"

To Reproduce:
Create a static cube inside schema folder. Fetch the data from Database and create dynamic schema. Now inside the dynamic schema try to create a relationship with the static cube using "belongs to". In the playground when you click the "dimensions" button, you will get a reference error.

Screenshot of the code as below :
image

Screenshot of the error as below :
image

Version Used : 0.22.3

@hassankhan hassankhan added the bug Something isn't working label Jan 26, 2021
@rongfengliang
Copy link
Contributor

@manickaws current you can use sqlAlias when dynamic schema generate include join. so join sql can use name directly
and needn't using reference. can do like this

const { transformDimensions, transformJoins, transformMeasures, convertStringPropToFunction } = require("./utils")
asyncModule(async () => {
  const dynamicCubes = [
    {
    "name": "payments",
    "sqlAlias": `payments`,
     "sql": "SELECT * FROM public.\"payments\"",
     "dataSource": "default",
     "joins": {
       "demoapp": {
         "relationship": `belongsTo`,
         "sql": "payments.__id = demoapp.__id"
       }
     },
     "measures": {
       "count": {
         "sql": "COUNT(DISTINCT \"payments\".__id)",
         "type": "number"
       },
       "total": {
         "sql": "\"payments\".\"total\"",
         "type": "sum"
       }
     },
     "dimensions": {
       "__id": {
         "sql": "__id",
         "type": "string",
         "primaryKey": true
       },
       "name": {
         "sql": "\"payments\".\"name\"",
         "type": "string"
       }
     }
   }, 
   {
      "name": "demoapp",
      "sqlAlias": `demoapp`,
      "sql": "SELECT * FROM public.\"demoapp\"",
     "dataSource": "default",
     "joins": {
       "demoapp": {
         "relationship": `belongsTo`,
         "sql": "payments.__id = demoapp.__id"
       }
     },
     "measures": {
       "count": {
         "sql": "COUNT(DISTINCT \"payments\".__id)",
         "type": "number"
       }
     },
     "dimensions": {
       "__id": {
         "sql": "__id",
         "type": "string",
         "primaryKey": true
       },
       "name": {
         "sql": "\"payments\".\"name\"",
         "type": "string"
       }
     }
 }
 ]
  dynamicCubes.forEach((dynamicCube) => {
    const dimensions = transformDimensions(dynamicCube.dimensions);
    const measures = transformMeasures(dynamicCube.measures);
    const joins = transformJoins(dynamicCube.joins);
    cube(dynamicCube.name, {
      sql: dynamicCube.sql,
      dimensions,
      measures,
      joins
    });
  });
}
)

@hassankhan hassankhan added the backend:server Issues relating to Cube Core's Server label May 5, 2021
@hassankhan
Copy link
Contributor

Hi @manickaws, just wanted to follow up and see if @rongfengliang's suggestion worked for you?

@diogosilva30
Copy link

how to do this in a view created with asyncModule?

Example:

asyncModule(async () => {
  // Create the objects for the view
  const viewObject = {
    title: i18n.gettext(`[Traffic Vertical]`),
    cubes: [
      // Calendar is the cube that "glues" all together
      {
        join_path: calendar,
        includes: "*",
        prefix: true,
      },
      // Counters
      {
        join_path: calendar.traffic_counter,
        includes: "*",
        prefix: true,
      },
   };
   
   if (someFunc("some_arg")) {
    view(`traffic`, viewObject);   

});

I'm dynamically creating views and cubes based on someFunc. However, this fails with:

ReferenceError: calendar is not defined
   at views/traffic.js:16:18

Any help?

@paveltiunov
Copy link
Member

@diogosilva30 You can use strings in join_path instead. Like join_path: 'calendar.traffic_counter'

@igorlukanin
Copy link
Member

Hi @manickaws 👋

I have just tried the following data model:

cube(`A`, {
  sql: `SELECT 123 AS value`,

  dimensions: {
    value: {
      sql: `value`,
      type: `number`,
      primary_key: true
    }
  }
})

asyncModule(async () => {
  cube(`B`, {
    sql: `SELECT 123 AS value`,

    joins: {
      A: {
        relationship: `one_to_one`,
        sql: `${CUBE.value} = ${A.value}`
      }
    },

    dimensions: {
      value: {
        sql: `value`,
        type: `number`,
        primary_key: true
      }
    }
  })
})

It generates correct SQL:

Screenshot 2024-10-09 at 14 55 39

So, here's how you can generate joins dynamically. Does this answer your question?

@igorlukanin igorlukanin self-assigned this Oct 9, 2024
@igorlukanin igorlukanin added question The issue is a question. Please use Stack Overflow for questions. data modeling and removed bug Something isn't working backend:server Issues relating to Cube Core's Server labels Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data modeling question The issue is a question. Please use Stack Overflow for questions.
Projects
None yet
Development

No branches or pull requests

7 participants