etrepat / baum

Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.
http://etrepat.com/baum
MIT License
2.24k stars 459 forks source link

ntegrity constraint violation: 1052 Column 'id' in where clause is ambiguous #183

Open blackgearit opened 8 years ago

blackgearit commented 8 years ago

In my Project I've referenced the model with baum in a hasToMany relation to another model for translations.

So, my Skills (baum) model hasMany SkillLocales

Eloquent raises this error when i use this to retrieve all Skills with locale

$skills = Skill::root()->descendants()->join('skills_locale','skills.id','=','skill_id')->where('skills_locale.locale',App::getLocale())->where('active',1)->get();

Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from skills inner join skills_locale on skills.id = skill_id where lft >= 1 and lft < 8 and id != 1 and skills_locale.locale = en and active = 1 order by skills.lft asc)

solved modifying /vendor/baum/baum/src/Baum/Node.php Line 471

from return $query->where($node->getKeyName(), '!=', $node->getKey()); to return $query->where($node->getTable().'.'.$node->getKeyName(), '!=', $node->getKey());

londoh commented 8 years ago

Hi blackgearit, I want to ask if you (anyone?) found a workaround for this?

I hit exactly this error with a very similar query.

thanks

l.

londoh commented 8 years ago

so I found something that seems to work:

By overriding scopeWithoutNode func to include the table name with the PK.

    public function scopeWithoutNode($query, $node)
    {
        return $query->where($this->table . '.' . $node->getKeyName(), '<>', $node->getKey());
    }

I dont know yet if there are relevant tests, OR more importantly - if this breaks anything else.

But in my limited testing it seems to do what I need, and it kinda makes sense. if anybody has any opinion or feedback on it please post.

Looking at the code around this also raises the question:

should there be a Node function to: "getQualifiedKeyColumnName()" ?

I'd be happy to submit a pr if its needed.

thanks

l.