laravel-oci8 icon indicating copy to clipboard operation
laravel-oci8 copied to clipboard

Relationships problem

Open jokeronaldo opened this issue 7 years ago • 20 comments

Summary of problem or feature request

Relationships are outputting stranger SQL, just does not work. Why it's writting IS NULL and IS NOT NULL over the query?

Code snippet of problem

MAIN MODEL

<?php
namespace Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Tgfpro extends Eloquent
{
    protected $table = 'TGFPRO';
    protected $primaryKey = 'CODPROD';
    public $timestamps = false;

    public function valor()
    {
    	return $this->hasMany('Models\Tgfexc', 'CODPROD')
    		->select('VLRVENDA');
    }

    public function getImagemAttribute($value)
    {
    	return base64_encode($value);
    }
}

SECONDARY MODEL

<?php
namespace Models;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Tgfexc extends Eloquent
{
    protected $table = 'TGFEXC';
    public $timestamps = false;
}

CONTROLLER CALL

        $produtos = Models\Tgfpro::where('ATIVO', 'S')
        	->limit(10)
        	->orderBy('CODPROD', 'ASC')
        	->get([
        		'CODPROD',
        		'DESCRPROD',
        		'IMAGEM'
        	]);

OUTPUTTED SQL

select "VLRVENDA" from "TGFEXC" where "TGFEXC"."CODPROD" is null and "TGFEXC"."CODPROD" is not null

System details

  • Windows 10
  • PHP 5.6.30
  • Using stand alone
  • Laravel-OCI8 ^5.4

jokeronaldo avatar Apr 19 '17 14:04 jokeronaldo

PS: When I try to access the list of relation items it returns empty.

jokeronaldo avatar Apr 19 '17 14:04 jokeronaldo

maybe your primary keys are different than defaults...

looks like its a problem on your side

mstaack avatar Apr 19 '17 16:04 mstaack

I had informed the primary key in model definition and the foreign key on hasMany. It's not the problem.

jokeronaldo avatar Apr 19 '17 16:04 jokeronaldo

@jokeronaldo the definition for this relationship is: return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

you have a select there also...why? put that to the calling place not in the relation defintion

mstaack avatar Apr 19 '17 18:04 mstaack

@mstaack If you look my MAIN MODEL, where I described my code, I point the following:

return $this->hasMany('Models\Tgfexc', 'CODPROD')

Where CODPROD is my foreign key and the last attribute is my local key, as I mentioned at main model code

protected $primaryKey = 'CODPROD';

Anyway, it's not the problem. The problem actually is something on query builder, if you look my sql output result you could see an ambiguous select is null and is not null.

jokeronaldo avatar Apr 19 '17 18:04 jokeronaldo

yeah i have seen your sql output...but what about this:

    public function valor()
    {
    	return $this->hasMany('Models\Tgfexc', 'CODPROD')
    		->select('VLRVENDA');
    }

select() shouldn't be there....

mstaack avatar Apr 19 '17 18:04 mstaack

@mstaack select should be there if you wnat, it's not a problem, i've tried with out it and I got same result.

About foreign_key, local_key, both got same name in both tables. Well, in fact it's a clue. I will try to alias the columns, but I doubt is that.

jokeronaldo avatar Apr 19 '17 19:04 jokeronaldo

@jokeronaldo ok sry then... for me relations work fine so far ;)

mstaack avatar Apr 19 '17 19:04 mstaack

@mstaack Thank you anyway =) I'm working on a legacy project, there's no id columns, its based on compound columns and the database map isn't human readable.

jokeronaldo avatar Apr 19 '17 19:04 jokeronaldo

uhhh..missing primary keys and compound columns....that will create problems ;)

mstaack avatar Apr 19 '17 19:04 mstaack

@jokeronaldo you might be using a global scope somewhere in your code that adds the null sql?

yajra avatar Apr 20 '17 06:04 yajra

@yajra nops.

jokeronaldo avatar Apr 20 '17 11:04 jokeronaldo

I have exactly the same problem, when the primary key is not integer, the sql is generated with 'is null' or 'is not null'! My relationships are correctly defined according Laravel documentation

fabriciopaulo avatar Jul 26 '17 19:07 fabriciopaulo

@fabriciopaulo I gave up to fix the problem, instead using relations, I done some black magic in code. Serious, I tried many different ways, nothing happens, nothing works.

jokeronaldo avatar Jul 27 '17 14:07 jokeronaldo

I do not get this issue, I have a project which has many existing tables. And primary keys are non integers. Probably a defect with 5.6 ? Will need to test your snippets to confirm.

Can you send here your create table query / DDL output?

Thanks

ChaosPower avatar Sep 19 '17 16:09 ChaosPower

i have a problem with relation hasMany public function posts(){ return $this->hasMany(Post::class,'LINKID','USERID'); }

when i run this code it return me LINKID = null $user->posts()->saveMany($data); @yajra

manhcuong2903 avatar Mar 20 '18 04:03 manhcuong2903

@jokeronaldo were you able to find the solution to this problem?

I am also having problems with the relationship between keys that are not integers.

paulosscruz avatar Mar 26 '18 12:03 paulosscruz

@paulosscruz I've left behind relations and do a normal query

jokeronaldo avatar Mar 26 '18 14:03 jokeronaldo

@jokeronaldo it's not a good performance for your query if you don't use relation

manhcuong2903 avatar Apr 06 '18 04:04 manhcuong2903

Hi, I got the exact problem,

Primary Model

<?php

namespace App\Modelos;

use Yajra\Oci8\Eloquent\OracleEloquent as Eloquent;

class Persona extends Eloquent
{
	protected $table = 'AI_PERSONA';
	protected $primaryKey  = 'PER_ID';
	public $timestamps = false;
	
	public function liceos()
	{
		return $this->hasMany('App\Modelos\Liceo','PER_ID');
	}
}

Second Model

<?php

namespace App\Modelos;

use Yajra\Oci8\Eloquent\OracleEloquent as Eloquent;

class Liceo extends Eloquent
{
    //
	protected $table = 'AI_RBD';
	protected $primaryKey = 'RBD_ID';
	public $timestamps = false;
}

Example en web.php

Route::get('test2', function() {	
	$persona = Persona::where('PER_ID',10)->with('liceos')->first();	
	dd ($persona);
});

Query ouput:

[2018-11-05 10:31:48] local.INFO: select * from (select * from "AI_PERSONA" where "PER_ID" = ?) where rownum = 1
[2018-11-05 10:31:48] local.INFO: array ( 0 => 10, )
[2018-11-05 10:31:48] local.INFO: select * from "AI_RBD" where "AI_RBD"."PER_ID" in (?)
[2018-11-05 10:31:48] local.INFO: array ( 0 => NULL, )

Somehow the Key is lost. I make another example in mysql and works fine.

System details

  • Windows 10
  • PHP 7.1.22
  • Using stand alone
  • Laravel 5.5.*
  • Laravel-OCI8 ^5.5.*

Problem solved, All the name of tables, camps, id, etc where change to lowercase, this solved the problem.

goten60 avatar Nov 05 '18 13:11 goten60

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Nov 30 '22 02:11 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Dec 08 '22 02:12 github-actions[bot]