Database_Exception [ 1146 ]: Table 'database.roles' doesn't exist [ SHOW FULL COLUMNS FROM `roles` ]

StackOverflow https://stackoverflow.com/questions/20448447

  •  30-08-2022
  •  | 
  •  

Question

I am currently setting up a new project and creating my login system. For some reason it seems to be searching for a roles table? This is my structure so far:

Controller

public function action_index()
{
    //Check login
    if ( Auth::instance()->logged_in() ):
        HTTP::redirect('/', 302);
    endif;

    if ( Request::current()->post() ):
        if( Auth::instance()->login( $_POST['username'], $_POST['password'] ) ):
            echo Debug::vars($_POST); exit;
        endif;
    endif;

    $view = View::factory('index/home');

    $index_page = $view->render();  
    $this->response->body($index_page);         
}

Model

class Model_User extends Model_Auth_User {

protected $_table_name = "users";
protected $_primary_val = "user_id";

protected $_table_columns = array(
'user_id'        => array('type' => 'int'),
'team_id'        => array('type' => 'int'),
'username'       => array('type' => 'string'),
'password'       => array('type' => 'string'),
);

And the error I am getting is

Database_Exception [ 1146 ]: Table 'database.roles' doesn't exist [ SHOW FULL COLUMNS FROM `roles` ]

Can anyone explain why it is looking for a table called roles? The table in the database is called users.

Was it helpful?

Solution

As you can see in the API browser, Model_Auth_User has the following has_many relationship:

"roles" => array(2) (
   "model" => string(4) "Role"
   "through" => string(11) "roles_users"
)

This is where the error originates from (as you extend said class).

In ORM's module folder you find the SQL schema used for default authentication:

modules/ORM/auth-schema-mysql.sql

CREATE TABLE IF NOT EXISTS `roles` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniq_name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation');
INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.');

CREATE TABLE IF NOT EXISTS `roles_users` (
  `user_id` int(10) UNSIGNED NOT NULL,
  `role_id` int(10) UNSIGNED NOT NULL,
  PRIMARY KEY  (`user_id`,`role_id`),
  KEY `fk_role_id` (`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `email` varchar(254) NOT NULL,
  `username` varchar(32) NOT NULL DEFAULT '',
  `password` varchar(64) NOT NULL,
  `logins` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `last_login` int(10) UNSIGNED,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniq_username` (`username`),
  UNIQUE KEY `uniq_email` (`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `user_tokens` (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` int(11) UNSIGNED NOT NULL,
  `user_agent` varchar(40) NOT NULL,
  `token` varchar(40) NOT NULL,
  `created` int(10) UNSIGNED NOT NULL,
  `expires` int(10) UNSIGNED NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniq_token` (`token`),
  KEY `fk_user_id` (`user_id`),
  KEY `expires` (`expires`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

ALTER TABLE `roles_users`
  ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE;

ALTER TABLE `user_tokens`
  ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top