Skip to content

$fields->getSelect() returns table.table.column #174

@kylekatarnls

Description

@kylekatarnls

Hello, I get an SQL error due to duplicate table name in qualified foreign key column:

I simplified code to get the root cause, here is my inspection:

public function resolve($root, $args, SelectFields $fields)
{
    return $fields->getSelect();
}

It gives me:

[
  "games.id",
  "games.name",
  "games.games.owner_id"
]

While I expected:

[
  "games.id",
  "games.name",
  "games.owner_id"
]

To get the SQL query working.

Here is what happens inside SelectFields.php:

$relation = call_user_func([app($parentType->config['model']), $key]);
// Add the foreign key here, if it's a 'belongsTo'/'belongsToMany' relation
if(method_exists($relation, 'getForeignKey'))
{
    $foreignKey = $relation->getForeignKey();
}
else if(method_exists($relation, 'getQualifiedForeignPivotKeyName'))
{
    $foreignKey = $relation->getQualifiedForeignPivotKeyName();
    // belongsToMany make PHP go there and $foreignKey is games.owner_id, cf getQualifiedForeignPivotKeyName() contents
}
else
{
    $foreignKey = $relation->getQualifiedForeignKeyName();
}

$foreignKey = $parentTable ? ($parentTable . '.' . $foreignKey) : $foreignKey;
// Then it prepend again the table name: games.games.owner_id

It seems to me like it should prepent $parentTable only if not getting a qualified name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions