Yii Framework Forum: CRUD Operations fails on example table "tbl_user" - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CRUD Operations fails on example table "tbl_user" CRUD Operations: Error: Table "tbl_user" does not have a prima Rate Topic: -----

#1 User is offline   Ola 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-April 10

Posted 11 April 2010 - 06:13 AM

Hi,

I try to generate crud methods from the quickstart docs: http://www.yiiframew...start.first-app but it fails complaining
about missing primary key.

First I created the table "tbl_user" from the mysql example.

Then in the yiic shell:
model User tbl_user
crud User

Fails with the error : "Error: Table "tbl_user" does not have a primary key."

My table looks like this:
mysql> describe tbl_user ;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(128) | NO | | NULL | |
| password | varchar(128) | NO | | NULL | |
| email | varchar(128) | NO | | NULL | |


php version is 5.2.8 and mysql version is 5.1.45


Any ideas ?

thanks ,
ola
0

#2 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 11 April 2010 - 10:01 AM

You need to set the id as a primary key (with auto increment)
0

#3 User is offline   Ola 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-April 10

Posted 11 April 2010 - 12:22 PM

I created the table primary key , and autoincrement:

CREATE TABLE tbl_user (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
username VARCHAR(128) NOT NULL,
password VARCHAR(128) NOT NULL,
email VARCHAR(128) NOT NULL
);


In the CrudCommand.php class, the primary key is null:

public function generateController($source,$params)
{
list($controllerClass,$modelClass)=$params;
$model=CActiveRecord::model($modelClass);

$id=$model->tableSchema->primaryKey;

// ---------------------------------
// This code fails throws exception:
// ---------------------------------
if($id===null)
throw new ShellException(Yii::t('yii','Error: Table "{table}" does not have a primary key.',array('{table}'=>$model->tableName())));



ola
0

#4 User is offline   Ola 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-April 10

Posted 11 April 2010 - 01:48 PM

Tried to debug some more: Wrote a small test to retreive metadata from the primary key, and can't see anything wrong:

$DB = new PDO("mysql:host=$hostname;dbname=mytestdatabase", $username, $password);
$select = $DB->query('SELECT id FROM tbl_user');
$meta = $select->getColumnMeta(0);
var_dump ( $meta);


Output is
array(7) {
["native_type"]=>
string(4) "LONG"
["flags"]=>
array(2) {
[0]=>
string(8) "not_null"
[1]=>
string(11) "primary_key"
}
["table"]=>
string(8) "tbl_user"
["name"]=>
string(2) "id"
["len"]=>
int(11)
["precision"]=>
int(0)
["pdo_type"]=>
int(2)
}
0

#5 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,875
  • Joined: 04-October 08
  • Location:DC, USA

Posted 12 April 2010 - 01:07 PM

That's very strange. Could you execute SQL "SHOW COLUMNS FROM tbl_user" and see the results?
Could you also exit the "yiic shell" and re-enter it to generate the model class?
0

#6 User is offline   Ola 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-April 10

Posted 13 April 2010 - 12:37 PM

Hi,

I'm using mysq:

mysql> desc tbl_user ;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(128) | NO | | NULL | |
| password | varchar(128) | NO | | NULL | |
| email | varchar(128) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+


ola
0

#7 User is offline   Zahid Mughal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 21-April 10
  • Location:Pakistan

Posted 26 April 2010 - 12:12 AM

View PostOla, on 11 April 2010 - 06:13 AM, said:

Hi,

I try to generate crud methods from the quickstart docs: http://www.yiiframew...start.first-app but it fails complaining
about missing primary key.

First I created the table "tbl_user" from the mysql example.

Then in the yiic shell:
model User tbl_user
crud User

Fails with the error : "Error: Table "tbl_user" does not have a primary key."



Dear use crud tbl_user istead of crud user, look on your data base table that how it is defined in database, your crud query depend on your table name.


My table looks like this:
mysql> describe tbl_user ;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(128) | NO | | NULL | |
| password | varchar(128) | NO | | NULL | |
| email | varchar(128) | NO | | NULL | |


php version is 5.2.8 and mysql version is 5.1.45


Any ideas ?

thanks ,
ola

Zahid Mughal

0

#8 User is offline   Zahid Mughal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 21-April 10
  • Location:Pakistan

Posted 26 April 2010 - 12:13 AM

View PostOla, on 11 April 2010 - 06:13 AM, said:

Hi,

I try to generate crud methods from the quickstart docs: http://www.yiiframew...start.first-app but it fails complaining
about missing primary key.

First I created the table "tbl_user" from the mysql example.

Then in the yiic shell:
model User tbl_user
crud User

Fails with the error : "Error: Table "tbl_user" does not have a primary key."






My table looks like this:
mysql> describe tbl_user ;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(128) | NO | | NULL | |
| password | varchar(128) | NO | | NULL | |
| email | varchar(128) | NO | | NULL | |


php version is 5.2.8 and mysql version is 5.1.45


Any ideas ?

thanks ,
ola



************************
Dear use crud tbl_user instead of crud user, look on your data base table that how it is defined in database, your crud query depend on your table name.
Zahid Mughal

0

#9 User is offline   Ola 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-April 10

Posted 26 April 2010 - 07:08 AM

I kind of get it to work with just using the table name:

model tbl_user
crud tbl_user

but the app blows up when i try to create a new user. And it also require me to type the auto increment id in the create user form wich
is not correct.


PHP Error
Description
Cannot modify header information - headers already sent by (output started at /usr/local/yii/framework/db/ar/CActiveRecord.php:327)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users