join-monster icon indicating copy to clipboard operation
join-monster copied to clipboard

SqlBatch junction column name conflict

Open bradzacher opened this issue 8 years ago • 4 comments

When doing a junction sqlBatch, the column aliases assigned by JM can conflict, causing hydration to fail.

Eg.

const one = new GraphQLObjectType({
    // ...
    uniqueKey: 'id'
    // ...
})

const two = new GraphQLObjectType({
    // ...
    fields: {
        type: GraphQLList(one),
        join: {
            junction: {
                uniqueKey: 'id',
                sqlTable: 'junction_tbl',
                sqlBatch: {
                    parentKey: 'parentKey',
                    thisKey: 'thisKey',
                    sqlJoin: () => '', // ...
                },
            },
        },
    },
    // ...
})

doing a selection on this results in this sql:

SELECT
  `two`.`id` AS `id`,
  `two`.`id` AS `parentKey`
FROM two `two`;
-- batch --
SELECT
  `junction_t`.`id` AS `id`,
  `one`.`id` AS `id`,
  `junction_t`.`thisKey` AS `thisKey`
FROM junction_tbl `junction_t`
LEFT JOIN one `one` ON // ...
WHERE `junction_t`.`thisKey` IN (66,70,71,72,79,80)

as you can see in the batched SQL statement, one.id is aliased as id, so does junction_t.id. So when the data is hydrated, it ignores the junction table id column because it gets overwritten by the join table id column.

bradzacher avatar Oct 02 '17 23:10 bradzacher

Commenting for visibility - experienced this exact same bug today. I think this has something to do with the aliased field's template string having a new line in it. The code does set-ify the select statements to make them unique, but the extra newline technically makes the select statement different, so the set conversion doesn't eliminate it.

EDIT : I think I might have solved a similar but different issue.

br3nd4nn34l avatar Nov 18 '17 01:11 br3nd4nn34l

a workaround we've found for this is to define the unique key as a composite of itself: uniqueKey: ['id', 'id'],

This will add the unique key to the query as

CONCAT(id, '#', id) AS `id#id`

Thus working around the conflict

bradzacher avatar Dec 07 '17 23:12 bradzacher

The uniqueKey: ['id', 'id'] workaround fixed it for me too- thanks.

tylerjbainbridge avatar Jan 25 '18 21:01 tylerjbainbridge

Can you provide a https://github.com/join-monster/join-monster-sscce?

nicoabie avatar May 05 '24 20:05 nicoabie