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


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

Here is my CI code:

$this->db->select('user_sections.*, section_types.name AS sectionType', FALSE);
$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:

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:

$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

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:

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??

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