Integrity constraint violation: 1048 Column 'id' cannot be null

I am getting the error: Integrity constraint violation: 1048 Column ‘id’ cannot be null.

Full application was generated using command:

composer create-project yiisoft/yii2-app-advanced advanced 2.0.3

I just added one table "tproducta" to database, generated model for it in the "backend" folder and run CRUD. CRUD generated controller in "backend\controllers" folder and several *.php files in "views\tproducta" folder including index.php, create.php, update.php, delete.php…

I try to insert row to the table using generated CRUD using url:

"http://localhost/TCyii/advanced/backend/web/index.php?r=tproducta%2Fcreate"

But i am getting error:

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘id’ cannot be null

The SQL being executed was: INSERT INTO tproducta (id, website, good_type, good_kwd1, good_kwd2, good_kwd3, good_title, good_manufacturer, good_price, good_price_cng1, good_price_cng2, good_price_cng3, good_expiration, good_techdetails, good_No_stock, good_NoOrders) VALUES (NULL, ‘jfhjgk’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, NULL, NULL, NULL, NULL, NULL, ‘’, NULL, NULL)

Error Info: Array

(

[0] => 23000


[1] => 1048


[2] => Column 'id' cannot be null

)

"

If i remove id from the _form.php deleting line:

<?= $form->field($model, ‘id’)->textInput() ?>

I am getting error: "Bad Request (#400), Missing required parameters: id ", although it is not required.

But the table row is created.

Than i try to create the second row in the table, i am getting error:

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘0’ for key ‘PRIMARY’

The SQL being executed was: INSERT INTO tproducta (website, good_type, good_kwd1, good_kwd2, good_kwd3, good_title, good_manufacturer, good_price, good_price_cng1, good_price_cng2, good_price_cng3, good_expiration, good_techdetails, good_No_stock, good_NoOrders) VALUES (’’, ‘’, ‘’, ‘’, ‘’, ‘jkgkj’, ‘’, NULL, NULL, NULL, NULL, NULL, ‘’, NULL, NULL)

Error Info: Array

(

[0] =&gt; 23000


[1] =&gt; 1062


[2] =&gt; Duplicate entry '0' for key 'PRIMARY'

)

"

In Tproducta controller there is the following rule related to id field:

C:\Bitnami\wampstack-5.4.38-0\apache2\htdocs\TCyii\advanced\backend\models\Tproducta.php


    public function rules()

    {

        return [

            //[['id'], 'required'],

            [['id', 'good_No_stock', 'good_NoOrders'], 'integer'],

How to tell to yii, not insert the primary key, leave this job to database?

The mistake was not in php files, but in the database. I forgot to set primary key to "AUTOINCREMENT".

Thus solution is to go to phpmyadmin "http://localhost/phpmyadmin/index.php"

Delete all rows in the table,

than edit field "id" and check box to "Auto Increment".

Than it is possible to submit the form, and rows appears in the table.

But after "create" form submission, although i can see submitted data in the table, i am still getting another error: "Not Found (#404)The requested page does not exist."

The automatically redirected url is (i mean after i press create button) : "http://localhost/TCyii/advanced/backend/web/index.php?r=tproducta%2Fview&id="

After the third row insertion using url:

"http://localhost/TCyii/advanced/backend/web/index.php?r=tproducta%2Fcreate"

the redirection url is generated correctly:

"http://localhost/TCyii/advanced/backend/web/index.php?r=tproducta%2Fview&id=3"

and there is no any errors