ujjwalguptaofficial / JsStore

Simplifying IndexedDB with SQL like syntax and promises
http://jsstore.net/
MIT License
858 stars 110 forks source link

Inner join between three tables fails #289

Closed femto-code closed 2 years ago

femto-code commented 2 years ago

I have three tables (transactions, labels, categories) that I would need to join for data acquisition. I use the following code:

var results = await connection.select({
    from: "transactions",
    join:[{
        with:"labels",
        on: "transactions.label=labels.id",
        as: {
            id: "labelsId"
        }
    },{
        with:"categories",
        on: "labels.category=categories.id",
        as: {
            id: "catId"
        }
    }]
});
console.log(results);

All three tables have the column id which is why I set the as property on both. But stillt - this does not work and gives error:

{message: 'column name exist in both table labels & categories', type: 'invalid_join_query'}

Apart from giving them individual id column names: what can I do about it? Thanks.

ujjwalguptaofficial commented 2 years ago

its clear from the exception thrown that column Name exist in table - labels & categories and that's why query can not be completed.

just rename column name using as option similar to how you are doing for id.

the below query should work -

var results = await connection.select({
    from: "transactions",
    join:[{
        with:"labels",
        on: "transactions.label=labels.id",
        as: {
            id: "labelsId",
            name:"labelsName"
        }
    },{
        with:"categories",
        on: "labels.category=categories.id",
        as: {
            id: "catId",
            name:'catName'
        }
    }]
});
console.log(results);
femto-code commented 2 years ago

Oh, sure, you're right - I didn't see that and just focused on the id column. Thanks!