Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Unknown column "id" in count process
  • Hi,

    I have model that handles with network and user relation. When I try to check whether there is any relation between them before inserting by using count() method, I get Unknown column "id".

    In my model configurations, there is many to many relation between networks and users model.

    Model_Network
    protected static $_many_many = array(
            'users' => array(
                'key_from' => 'id',
                'table_through' => 'networks_users',
                'key_through_from' => 'network_id',
                'key_through_to' => 'user_id',
                'model_to' => 'Model_User',
                'key_to' => 'id',
                'cascade_save' => true,
                'cascade_delete' => false
            )
        );

    Model_User
    protected static $_many_many = array(
            'networks' => array(
                'key_from' => 'id',
                'table_through' => 'networks_users',
                'key_through_from' => 'user_id',
                'key_through_to' => 'network_id',
                'model_to' => 'Model_Network',
                'key_to' => 'id',
                'cascade_save' => true,
                'cascade_delete' => false
            )
        );

    Model_NetwokUserRelation
    protected static $_properties = array(
    'network_id',
    'user_id',
    'created_at'
    );


    When I run the query below,

    $find = Model_NetworkUserRelation::query()->where(array('network_id' => $networkId, 'user_id' => $userId))->count();

    Column not found: 1054 Unknown column 't0.id' in 'field list' with query: "SELECT COUNT(DISTINCT `t0`.`id`) AS count_result FROM `networks_users` AS `t0` WHERE `t0`.`network_id` = '4' AND `t0`.`user_id` = 1"

    There's no id column in my networks_user table. So, which is wrong? Was built query or my relations?
  • You need to add a id to your networks_user table

      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

    The error is because all tables should have a primary key of id

  • HarroHarro
    Accepted Answer
    It doesn't have to be 'id', but indeed every Model must have a primary key, and by default that is 'id'.
  • Yep you are right Harro, Im just in the habit of always giving my tables a primary auto_increment id field.

  • Thanks guys. I should define a primary key.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion