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

one-of-many relationship query error. #37402

Closed
eusonlito opened this issue May 18, 2021 · 3 comments · Fixed by #37411
Closed

one-of-many relationship query error. #37402

eusonlito opened this issue May 18, 2021 · 3 comments · Fixed by #37411

Comments

@eusonlito
Copy link
Contributor

eusonlito commented May 18, 2021

  • Laravel Version: v8.42.0
  • PHP Version: 7.4.18
  • Database Driver & Version: MySQL 8.0

Description:

Related to merged PR #37362

Having this tables:

Schema::create('exchange', function (Blueprint $table) {
    $table->id();

    $table->unsignedFloat('exchange', 19, 12);

    $table->timestamps();

    $table->unsignedBigInteger('platform_id');
    $table->unsignedBigInteger('product_id');
});

Schema::create('product', function (Blueprint $table) {
    $table->id();

    $table->string('code')->index();
    $table->string('name');

    $table->unsignedBigInteger('platform_id');
});

Product > Exchange relation is set as:

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function exchange(): HasOne
{
    return $this->hasOne(Exchange::class, 'product_id');
}

My code is:

\App\Models\Product::with(['exchange'])->limit(1)->get();
select * from `exchange` where `exchange`.`product_id` in (1)

After adding ofMany to hasOne

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function exchange(): HasOne
{
    return $this->hasOne(ExchangeModel::class, static::$foreign)->ofMany();
}

same previous code generate an SQL error:

Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'exchange.product_id' in where clause is ambiguous (SQL: select * from `exchange` inner join (select MAX(id) as id, `exchange`.`product_id` from `exchange` group by `exchange`.`product_id`) as `exchange` on `exchange`.`id` = `exchange`.`id` and `exchange`.`product_id` = `exchange`.`product_id` where `exchange`.`product_id` in (1))
@driesvints
Copy link
Member

Ping @cbl

@cbl
Copy link
Contributor

cbl commented May 18, 2021

The join alias ist the same as the table name both are exchange. Try setting a different alias:

->ofMany(relation: 'latest_exchange');

Or

->ofMany('id', 'max', 'latest_exchange');

@eusonlito
Copy link
Contributor Author

@cbl it works perfect.

Maybe this can be a common "error". What about to rename default alias as table_name + _join|_related or something similar to avoid issues like this?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants