Welcome Guest, Not a member yet? Register   Sign In
db join gives 'not unique alias' error [SOLVED]
#1

[eluser]nilsm[/eluser]
Hello,

when I do a join without assigning aliases to the table names, I get a SQL error:

Here is my CI code:

Code:
$this->db->select('user_sections.*, section_types.name AS sectionType', FALSE);
$this->db->from('user_sections');
$this->db->join('section_types', 'user_sections.typeid = section_types.id');

and here is the SQL error. See how it doubles up on table names in the 'from' part of the query? That is what is causing the error and I'm not sure why it's happening:

Code:
Not unique table/alias: 'user_sections'

SELECT user_sections.*, section_types.name AS sectionType FROM (user_sections, user_sections) JOIN section_types ON user_sections.typeid = section_types.id

I can workaround by assigning aliases to the table names in the 'from' and 'join' sections but it seems like a CI bug or perhaps a misunderstanding on my part, so was hoping there was another fix.

This code works:

Code:
$this->db->select('a.*, b.name AS sectionType', FALSE);
$this->db->from('user_sections as a');
$this->db->join('section_types as b', 'a.typeid = b.id');

Thanks - Nils
#2

[eluser]nilsm[/eluser]
I also just realised that even with aliases, the CI-generated query still doubles up on table names in the 'FROM' block. The example above generates this SQL:

Code:
FROM (user_sections as a, user_sections)

It doesn't fail anymore because the duplicate is resolved via the alias - but why the double-up in the first place??
#3

[eluser]nilsm[/eluser]
I'm really sorry but I spotted my mistake - I still had the table name in the final '$this->db->get()' clause! That was causing the double-up in the FROM clause and hence the error messages.




Theme © iAndrew 2016 - Forum software by © MyBB