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

Update failed with hasManyThrough relationship #25740

Closed
AminulBD opened this issue Sep 22, 2018 · 7 comments
Closed

Update failed with hasManyThrough relationship #25740

AminulBD opened this issue Sep 22, 2018 · 7 comments

Comments

@AminulBD
Copy link
Contributor

AminulBD commented Sep 22, 2018

  • Laravel Version: v5.6.38
  • PHP Version: 7.2.8
  • Database Driver & Version: MySQL v5.7.23

Description:

I've made a relationship with hasManyThrough and it's working perfectly to fetch data. But, When I'm trying to update I get this error below.

Integrity constraint violation: 1052 Column 'customer_id' in field list is ambiguous (SQL: update `invoice_items` inner join `invoices` on `invoices`.`id` = `invoice_items`.`invoice_id` set `customer_id` = 2, `user_id` = 7, `updated_at` = 2018-09-22 21:47:15 where `invoices`.`order_id` = 17)

Steps To Reproduce:

Tables structures:

orders
- id
- user_id
- customer_id
- content
invoices
- id
- order_id
- user_id
- customer_id
- content
transactions
- id
- invoice_id
- user_id
- customer_id
- content

Relationships

public function transactions()
{
   return $this->hasManyThrough('App\Transaction', 'App\Invoice');
}

What i expected to work

$order->transactions()->update([
    'customer_id' => 100,
    'user_id' => 100
]);
@AminulBD
Copy link
Contributor Author

I've found a temporary solution about this issue like this:

foreach ($order->transactions as $transaction) {
    $transaction->update($updateRules);
}

@staudenmeir
Copy link
Contributor

Specify the table name:

$order->transactions()->update([
    'transactions.customer_id' => 100,
    'transactions.user_id' => 100
]);

@AminulBD
Copy link
Contributor Author

@standaniels Thank you. But, you know if I specified the table name but the problem still exists because the updated_at which can't be set by specific table name.

Have a look at this error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous (SQL: update `invoice_items` inner join `invoices` on `invoices`.`id` = `invoice_items`.`invoice_id` set `invoice_items`.`customer_id` = 2, `invoice_items`.`user_id` = 3, `invoice_items`.`updated_at` = 2018-09-23 21:33:54, `updated_at` = 2018-09-23 21:33:54 where `invoices`.`order_id` = 25)

I've experimented with below code by adding transactions.updated_at => now()->toDateTimeString(). Still no luck.

@staudenmeir
Copy link
Contributor

This is a more complex issue, see #13909.

@staudenmeir
Copy link
Contributor

You can use this workaround:

$order->transactions()->toBase()->update([
    'transactions.customer_id' => 100,
    'transactions.user_id' => 100,
    'transactions.updated_at' => now()
]);

@staudenmeir
Copy link
Contributor

Please close the issue.

@staudenmeir
Copy link
Contributor

Will be fixed in Laravel 5.8: #26031

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

No branches or pull requests

3 participants