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

Self relationship generate Error ORA-00904: "LARAVEL_RESERVED_0" #886

Closed
ErDiabIo opened this issue Sep 2, 2024 · 3 comments · Fixed by #889
Closed

Self relationship generate Error ORA-00904: "LARAVEL_RESERVED_0" #886

ErDiabIo opened this issue Sep 2, 2024 · 3 comments · Fixed by #889

Comments

@ErDiabIo
Copy link
Contributor

ErDiabIo commented Sep 2, 2024

Hi

when query a model with whereDoesntHave on self relationship generete the following error

Error Code : 904 Error Message : ORA-00904: "CE_LARAVEL_RESERVED_0"."ID": invalid identifier Position : 103 Statement : select * from "CE_CATEGORIES" where not exists (select * from "CE_CATEGORIES" laravel_reserved_0 where "CE_LARAVEL_RESERVED_0"."ID" = "CE_CATEGORIES"."PARENT_CATEGORY")

For now i fix the problem by editing the following function wrapTable
inside src/Oci8/Query/Grammars/OracleGrammar.php
for use the prefix when concatenate $segments[0] with $segments[1]

  public function wrapTable($table)
  {
      if ($this->isExpression($table)) {
          return $this->getValue($table);
      }

      if (strpos(strtolower($table), ' as ') !== false) {
          $table = str_replace(' as ', ' ', strtolower($table));
      }

      $tableName = $this->wrap($this->tablePrefix.$table, true);
      $segments = explode(' ', $table);
      if (count($segments) > 1) {
          $tableName = $this->wrap($this->tablePrefix.$segments[0]).' '.$this->tablePrefix.$segments[1];
      }

      return $this->getSchemaPrefix().$tableName;
  }

Below an example code

Set the .env variable DB_PREFIX

Model

<?php

namespace App\Models\Category;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Category extends Model
{
    use HasFactory;

    /**
     * @return BelongsTo<Category>
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(Category::class, 'parent_category');
    }
}

Controller

$categories = Category::whereDoesntHave('parent')->get();
@ErDiabIo ErDiabIo changed the title Self relationship generate Error ORA-00904: "CE_LARAVEL_RESERVED_0" Self relationship generate Error ORA-00904: "LARAVEL_RESERVED_0" Sep 2, 2024
@yajra
Copy link
Owner

yajra commented Sep 5, 2024

Thanks for reporting, wrapTable indeed needs several updates. Some fixes were implemented at #879.

Would you be able to check if #879 covers this issue? Will also check further when I got the chance.

@ErDiabIo
Copy link
Contributor Author

ErDiabIo commented Sep 5, 2024

Hi,

I’ve checked, and issue #879 doesn’t cover this case.
I can create a PR to fix this problem.

@yajra
Copy link
Owner

yajra commented Sep 5, 2024

Yes please, PR is appreciated. Thanks!

@yajra yajra closed this as completed in #889 Sep 7, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants