Wednesday, July 13, 2011

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'u.id' in 'field list' (Errors & Solutions - #20)

Error

I got this error while selecting records from a View using Symfony framework ( Doctrine )

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'u.id' in 'field list'

Solution

It seems that Doctrine assumes an column named "ID" as primary key if it didn't find one, specially if you are selecting from a view, so to fix that i

1- Copied the "setTableDefinition" function from the base table class to the main table class in the model
2- Commented out the already generated ID column
3- Select other -appropriate- column(s) to be the primary key


public function setTableDefinition()
{

$this->setTableName('TABLE_NAME');

/*
$this->hasColumn('id', 'integer', 8, array(
'type' => 'integer',
'autoincrement' => true,
'primary' => true,
'length' => 8,
));
*/

$this->hasColumn('user_field', 'integer', 8, array(
'type' => 'integer',
'fixed' => 0,
'unsigned' => false,
'primary' => true,
'notnull' => false,
'autoincrement' => false,
'length' => 8,
));

}


2 comments:

  1. I had the same problem. Now thanks to you I know that I have to add the id field to the view.
    Thanks.

    Now I have this new problem: "The user provider must return a UserInterface object".
    Will I ever be able to finish the login process? Who knows.

    ReplyDelete
    Replies
    1. I assume that you are using Laravel. You need to implements UserInterface at your User model.

      Delete