Integrity constraint violation – yii\db\IntegrityException

Hello,

I have just installed the advanced template, used the singup field ok for the first user but then when I try to add a second user I get this error, I cannot work out why you would get a duplicate on the id which is set to INT(11)

Here is the error. thank you:

`




Integrity constraint violation – yii\db\IntegrityException


SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'

The SQL being executed was: INSERT INTO `user` (`username`, `email`, `password_hash`, `auth_key`, `status_id`, `role_id`, `user_type_id`, `created_at`, `updated_at`) VALUES ('benoit3', '44321321@hotmail.com', '$2y$13$qRpSesNVYwuY1PUDFMu1v.5d9CcA.cRjlTefTGosoiFOPncjxn/4C', 'PtxahtBvxaTQEJZBVoe8tbb0NG5siYXF', 10, 10, 10, NOW(), NOW())

Error Info: Array

(

    [0] => 23000

    [1] => 1062

    [2] => Duplicate entry '0' for key 'PRIMARY'

)

↵

Caused by: PDOException


SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'


in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\db\Command.php at line 768

1. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\db\Schema.php at line 532

523524525526527528529530531532533534535536537538539540541 

        $exceptionClass = '\yii\db\Exception';

        foreach ($this->exceptionMap as $error => $class) {

            if (strpos($e->getMessage(), $error) !== false) {

                $exceptionClass = $class;

            }

        }

        $message = $e->getMessage()  . "\nThe SQL being executed was: $rawSql";

        $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;

        return new $exceptionClass($message, $errorInfo, (int) $e->getCode(), $e);

    }

 

    /**

     * Returns a value indicating whether a SQL statement is for read purpose.

     * @param string $sql the SQL statement

     * @return boolean whether a SQL statement is for read purpose.

     */

    public function isReadQuery($sql)

    {

2. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\db\Command.php – yii\db\Schema::convertException(PDOException, 'INSERT INTO `user` (`username`, ...') at line 776

3. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\db\ActiveRecord.php – yii\db\Command::execute() at line 452

4. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\db\ActiveRecord.php – yii\db\ActiveRecord::insertInternal(null) at line 427

5. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\db\BaseActiveRecord.php – yii\db\ActiveRecord::insert(true, null) at line 582

6. in C:\xampp\htdocs\yii2build\frontend\models\SignupForm.php – yii\db\BaseActiveRecord::save() at line 51

45464748495051525354555657        if ($this->validate()) {

            $user = new User();

            $user->username = $this->username;

            $user->email = $this->email;

            $user->setPassword($this->password);

            $user->generateAuthKey();

            $user->save();

            return $user;

        }

 

        return null;

    }

}

7. in C:\xampp\htdocs\yii2build\frontend\controllers\SiteController.php – frontend\models\SignupForm::signup() at line 123

117118119120121122123124125126127128129    }

 

    public function actionSignup()

    {

        $model = new SignupForm();

        if ($model->load(Yii::$app->request->post())) {

            if ($user = $model->signup()) {

                if (Yii::$app->getUser()->login($user)) {

                    return $this->goHome();

                }

            }

        }

 

8. frontend\controllers\SiteController::actionSignup()

9. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\base\InlineAction.php – call_user_func_array([frontend\controllers\SiteController, 'actionSignup'], []) at line 55

10. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\base\Controller.php – yii\base\InlineAction::runWithParams(['r' => 'site/signup']) at line 151

145146147148149150151152153154155156157        }

 

        $result = null;

 

        if ($runAction && $this->beforeAction($action)) {

            // run the action

            $result = $action->runWithParams($params);

 

            $result = $this->afterAction($action, $result);

 

            // call afterAction on modules

            foreach ($modules as $module) {

                /* @var $module Module */

11. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\base\Module.php – yii\base\Controller::runAction('signup', ['r' => 'site/signup']) at line 455

12. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\web\Application.php – yii\base\Module::runAction('site/signup', ['r' => 'site/signup']) at line 83

13. in C:\xampp\htdocs\yii2build\vendor\yiisoft\yii2\base\Application.php – yii\web\Application::handleRequest(yii\web\Request) at line 375

14. in C:\xampp\htdocs\yii2build\frontend\web\index.php – yii\base\Application::run() at line 18

12131415161718    require(__DIR__ . '/../../common/config/main-local.php'),

    require(__DIR__ . '/../config/main.php'),

    require(__DIR__ . '/../config/main-local.php')

);

 

$application = new yii\web\Application($config);

$application->run();

 $_GET = [

    'r' => 'site/signup',

];


$_POST = [

    '_csrf' => 'UWppTHNBLVUbPQguIRkaHBUAACheOBk.GCkKCiY0WAIDBSsKCwh1Ag==',

    'SignupForm' => [

        'username' => 'benoit3',

        'email' => '44321321@hotmail.com',

        'password' => '123456789',

    ],

    'signup-button' => '',

];


$_SERVER = [

    'MIBDIRS' => 'C:/xampp/php/extras/mibs',

    'MYSQL_HOME' => '\\xampp\\mysql\\bin',

    'OPENSSL_CONF' => 'C:/xampp/apache/bin/openssl.cnf',

    'PHP_PEAR_SYSCONF_DIR' => '\\xampp\\php',

    'PHPRC' => '\\xampp\\php',

    'TMP' => '\\xampp\\tmp',

    'HTTP_HOST' => 'yii2build.com',

    'HTTP_CONNECTION' => 'keep-alive',

    'CONTENT_LENGTH' => '194',

    'HTTP_CACHE_CONTROL' => 'max-age=0',

    'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',

    'HTTP_ORIGIN' => 'http://yii2build.com',

    'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',

    'CONTENT_TYPE' => 'application/x-www-form-urlencoded',

    'HTTP_REFERER' => 'http://yii2build.com/index.php?r=site%2Fsignup',

    'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',

    'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',

    'HTTP_COOKIE' => '_csrf=21e5fd76b7cd07b5aad452b30119e8e69003883a2e257ec2e7266f85e2b58839s%3A32%3A%22JWabRX7IDjid-y4kICcFUuuWRoBFxIXW%22%3B; PHPSESSID=17515dp4026qav907u621mpif0',

    'PATH' => 'C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Intel\\OpenCL SDK\\2.0\\bin\\x86;C:\\Program Files (x86)\\Intel\\OpenCL SDK\\2.0\\bin\\x64;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Windows Kits\\8.0\\Windows Performance Toolkit\\;C:\\xampp\\php;C:\\ProgramData\\ComposerSetup\\bin',

    'SystemRoot' => 'C:\\WINDOWS',

    'COMSPEC' => 'C:\\WINDOWS\\system32\\cmd.exe',

    'PATHEXT' => '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',

    'WINDIR' => 'C:\\WINDOWS',

    'SERVER_SIGNATURE' => '

Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3 Server at yii2build.com Port 80


',

    'SERVER_SOFTWARE' => 'Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3',

    'SERVER_NAME' => 'yii2build.com',

    'SERVER_ADDR' => '127.0.0.1',

    'SERVER_PORT' => '80',

    'REMOTE_ADDR' => '127.0.0.1',

    'DOCUMENT_ROOT' => 'C:/xampp/htdocs/yii2build/frontend/web',

    'REQUEST_SCHEME' => 'http',

    'CONTEXT_PREFIX' => '',

    'CONTEXT_DOCUMENT_ROOT' => 'C:/xampp/htdocs/yii2build/frontend/web',

    'SERVER_ADMIN' => 'postmaster@localhost',

    'SCRIPT_FILENAME' => 'C:/xampp/htdocs/yii2build/frontend/web/index.php',

    'REMOTE_PORT' => '61011',

    'GATEWAY_INTERFACE' => 'CGI/1.1',

    'SERVER_PROTOCOL' => 'HTTP/1.1',

    'REQUEST_METHOD' => 'POST',

    'QUERY_STRING' => 'r=site%2Fsignup',

    'REQUEST_URI' => '/index.php?r=site%2Fsignup',

    'SCRIPT_NAME' => '/index.php',

    'PHP_SELF' => '/index.php',

    'REQUEST_TIME_FLOAT' => 1418855701.0959999561309814453125,

    'REQUEST_TIME' => 1418855701,

];


$_COOKIE = [

    '_csrf' => '21e5fd76b7cd07b5aad452b30119e8e69003883a2e257ec2e7266f85e2b58839s:32:"JWabRX7IDjid-y4kICcFUuuWRoBFxIXW";',

    'PHPSESSID' => '17515dp4026qav907u621mpif0',

];


$_SESSION = [

    '__flash' => [],

    '__captcha/site/captcha' => 'wasuzli',

    '__captcha/site/captchacount' => 1,

    '__id' => null,

];

Yii Framework

2014-12-17, 23:35:02


Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3

Yii Framework/2.0.0






Thanks!

Ben

It looks like your primary key isn’t set to auto increment, and as such is defaulting to 0. As you’re not actually specifying a primary key value on insert (as you assume it will auto increment), as soon as you attempt to enter a second row with the default value it will error.

If you have not already done so, you want to make sure that you ran the migration as per the instructions for the setup. Otherwise you could have problems with the data structure and defaults.

thanks to both of you, yes you were right, the id was not set to auto increment.

It is now working :slight_smile:

Ben

how I can capture that error and throw an exception?

I have similar problem (can not add second user), but it seems not because the id isn’t set to auto increment yet. when I look the DB design, the id is already set to auto increment. Can somebody help me? This is the error message.

Note: the user table is not modified from yii2 advanced template default migration. I use ms sql server for the DB.




SQLSTATE[23000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Violation of UNIQUE KEY constraint 'UQ__user__665877D019DEDCC2'. Cannot insert duplicate key in object 'dbo.user'. The duplicate key value is (<NULL>).

The SQL being executed was: INSERT INTO [user] ([username], [email], [password_hash], [auth_key], [status], [created_at], [updated_at]) VALUES ('tari', 'lestari.ekowahyu@gmail.com', '$2y$13$asAfoCF/HJK8TKSsirCiy.geCN0MQuhWAHWvwcPnqAX.h.XP8xcz.', '35TlB2fCC0RH29RC9BKZFCGmodnrH_eS', 10, 1463321535, 1463321535)


Error Info: Array

(

    [0] => 23000

    [1] => 2627

    [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Violation of UNIQUE KEY constraint 'UQ__user__665877D019DEDCC2'. Cannot insert duplicate key in object 'dbo.user'. The duplicate key value is ().

)


↵

Caused by: PDOException

SQLSTATE[23000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Violation of UNIQUE KEY constraint 'UQ__user__665877D019DEDCC2'. Cannot insert duplicate key in object 'dbo.user'. The duplicate key value is (<NULL>).


in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\db\Command.php at line 844


    1. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\db\Schema.php at line 633

    624625626627628629630631632633634635636637638639640641642


     

            $exceptionClass = '\yii\db\Exception';

            foreach ($this->exceptionMap as $error => $class) {

                if (strpos($e->getMessage(), $error) !== false) {

                    $exceptionClass = $class;

                }

            }

            $message = $e->getMessage()  . "\nThe SQL being executed was: $rawSql";

            $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;

            return new $exceptionClass($message, $errorInfo, (int) $e->getCode(), $e);

        }

     

        /**

         * Returns a value indicating whether a SQL statement is for read purpose.

         * @param string $sql the SQL statement

         * @return boolean whether a SQL statement is for read purpose.

         */

        public function isReadQuery($sql)

        {


    2. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\db\Command.php at line 854 – yii\db\Schema::convertException(PDOException, 'INSERT INTO [user] ([username], ...')

    3. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\db\Schema.php at line 451 – yii\db\Command::execute()

    4. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\db\ActiveRecord.php at line 457 – yii\db\Schema::insert('{{%user}}', ['username' => 'tari', 'email' => 'lestari.ekowahyu@gmail.com', 'password_hash' => '$2y$13$asAfoCF/HJK8TKSsirCiy.geC...', 'auth_key' => '35TlB2fCC0RH29RC9BKZFCGmodnrH_eS', ...])

    5. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\db\ActiveRecord.php at line 427 – yii\db\ActiveRecord::insertInternal(null)

    6. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\db\BaseActiveRecord.php at line 598 – yii\db\ActiveRecord::insert(true, null)

    7. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\frontend\models\SignupForm.php at line 56 – yii\db\BaseActiveRecord::save()

    505152535455565758


            $user = new User();

            $user->username = $this->username;

            $user->email = $this->email;

            $user->setPassword($this->password);

            $user->generateAuthKey();

     

            return $user->save() ? $user : null;

        }

    }


    8. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\frontend\controllers\SiteController.php at line 158 – frontend\models\SignupForm::signup()

    152153154155156157158159160161162163164


         * @return mixed

         */

        public function actionSignup()

        {

            $model = new SignupForm();

            if ($model->load(Yii::$app->request->post())) {

                if ($user = $model->signup()) {

                    if (Yii::$app->getUser()->login($user)) {

                        return $this->goHome();

                    }

                }

            }

     


    9. frontend\controllers\SiteController::actionSignup()

    10. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\base\InlineAction.php at line 55 – call_user_func_array([frontend\controllers\SiteController, 'actionSignup'], [])

    11. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\base\Controller.php at line 154 – yii\base\InlineAction::runWithParams(['r' => 'site/signup'])

    12. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\base\Module.php at line 454 – yii\base\Controller::runAction('signup', ['r' => 'site/signup'])

    13. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\web\Application.php at line 84 – yii\base\Module::runAction('site/signup', ['r' => 'site/signup'])

    14. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\vendor\yiisoft\yii2\base\Application.php at line 375 – yii\web\Application::handleRequest(yii\web\Request)

    15. in E:\Serverku\xampp\htdocs\SBR\SBR-advanced\frontend\web\index.php at line 18 – yii\base\Application::run()

    12131415161718


        require(__DIR__ . '/../../common/config/main-local.php'),

        require(__DIR__ . '/../config/main.php'),

        require(__DIR__ . '/../config/main-local.php')

    );

     

    $application = new yii\web\Application($config);

    $application->run();


$_GET = [

    'r' => 'site/signup',

];


$_POST = [

    '_csrf' => 'eUxkMDNTd3Q4BiZ9UTs8HS58V0cDYzMELQ0zc3IlJS4TOjNKdGs9DA==',

    'SignupForm' => [

        'username' => 'tari',

        'email' => 'lestari.ekowahyu@gmail.com',

        'password' => 'pass123456',

    ],

    'signup-button' => '',

];


$_COOKIE = [

    'PHPSESSID' => 'frnv3ru0l3ct2tp89gilef1hh3',

    '_csrf' => 'f4b6aaf0a8f86c321865196c1d67c143d3bd831fd64632517fbc2ce1947b2751a:2:{i:0;s:5:"_csrf";i:1;s:32:"AJBMbhKiW03w00DpTAWCAvRZjvWzG8Jx";}',

];


$_SESSION = [

    '__flash' => [],

];




I’ve asked in another forum and told that

is it not possible to have column that allow null but should unique if the value is not null?

i have also same issue.

Integrity constraint violation – yii\db\IntegrityException

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (maintances.maintances, CONSTRAINT maintances_ibfk_1 FOREIGN KEY (asset_id) REFERENCES asset (id))

The SQL being executed was: INSERT INTO maintances (maintance_by, maintance_purchase, total_cost_invoked, reference_bill, notes) VALUES (‘pp’, ‘pp’, ‘555’, ‘12’, ‘jhgj’)

Error Info: Array

(

[0] =&gt; 23000


[1] =&gt; 1452


[2] =&gt; Cannot add or update a child row: a foreign key constraint fails (`maintances`.`maintances`, CONSTRAINT `maintances_ibfk_1` FOREIGN KEY (`asset_id`) REFERENCES `asset` (`id`))

)

Caused by: PDOException

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (maintances.maintances, CONSTRAINT maintances_ibfk_1 FOREIGN KEY (asset_id) REFERENCES asset (id))

in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Command.php at line 846

  1. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Schema.php at line 636

627628629630631632633634635636637638639640641642643644645

    &#036;exceptionClass = '&#092;yii&#092;db&#092;Exception';


    foreach (&#036;this-&gt;exceptionMap as &#036;error =&gt; &#036;class) {


        if (strpos(&#036;e-&gt;getMessage(), &#036;error) &#33;== false) {


            &#036;exceptionClass = &#036;class;


        }


    }


    &#036;message = &#036;e-&gt;getMessage()  . &quot;&#092;nThe SQL being executed was: &#036;rawSql&quot;;


    &#036;errorInfo = &#036;e instanceof &#092;PDOException ? &#036;e-&gt;errorInfo : null;


    return new &#036;exceptionClass(&#036;message, &#036;errorInfo, (int) &#036;e-&gt;getCode(), &#036;e);


}





/**


 * Returns a value indicating whether a SQL statement is for read purpose.


 * @param string &#036;sql the SQL statement


 * @return bool whether a SQL statement is for read purpose.


 */


public function isReadQuery(&#036;sql)


{
  1. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Command.php at line 856 – yii\db\Schema::convertException(PDOException, ‘INSERT INTO maintances (`maint…’)

  2. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Schema.php at line 454 – yii\db\Command::execute()

  3. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\ActiveRecord.php at line 494 – yii\db\Schema::insert(‘maintances’, [‘maintance_by’ => ‘pp’, ‘maintance_purchase’ => ‘pp’, ‘total_cost_invoked’ => ‘555’, ‘reference_bill’ => ‘12’, …])

  4. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\ActiveRecord.php at line 461 – yii\db\ActiveRecord::insertInternal(null)

  5. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\BaseActiveRecord.php at line 635 – yii\db\ActiveRecord::insert(true, null)

  6. in C:\xxampp\htdocs\maintan\backend\controllers\MaintancesController.php at line 76 – yii\db\BaseActiveRecord::save()

70717273747576777879808182 if ($model->load(Yii::$app->request->post())) {

    //&#036;model-&gt;asset_id=&#036;get['asset_id'];








//print_r(&#036;model);


    &#036;model-&gt;save();





    return &#036;this-&gt;redirect(['view', 'id'=&gt; &#036;model-&gt;id]);





    } else {


        return &#036;this-&gt;render('create', [


            'model' =&gt; &#036;model,
  1. backend\controllers\MaintancesController::actionCreate()

  2. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\InlineAction.php at line 57 – call_user_func_array([backend\controllers\MaintancesController, ‘actionCreate’], [])

  3. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\Controller.php at line 156 – yii\base\InlineAction::runWithParams([‘r’ => ‘maintances/create’, ‘asset_id’ => ‘30’])

  4. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\Module.php at line 523 – yii\base\Controller::runAction(‘create’, [‘r’ => ‘maintances/create’, ‘asset_id’ => ‘30’])

  5. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\web\Application.php at line 102 – yii\base\Module::runAction(‘maintances/create’, [‘r’ => ‘maintances/create’, ‘asset_id’ => ‘30’])

  6. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\Application.php at line 380 – yii\web\Application::handleRequest(yii\web\Request)

  7. in C:\xxampp\htdocs\maintan\backend\web\index.php at line 17 – yii\base\Application::run()

11121314151617 require(DIR . ‘/../../common/config/main.php’),

require(__DIR__ . '/&#46;&#46;/&#46;&#46;/common/config/main-local.php'),


require(__DIR__ . '/&#46;&#46;/config/main.php'),


require(__DIR__ . '/&#46;&#46;/config/main-local.php')

);

(new yii\web\Application($config))->run();

$_GET = [

'r' =&gt; 'maintances/create',


'asset_id' =&gt; '30',

];

$_POST = [

'_csrf-backend' =&gt; 'bXBKX1lfM2YYKAJyGy12Eh8RfwYMLHhRFSkTMDgFAiwjHww5FQxXNA==',


'repair_date-maintances-repair_date-disp' =&gt; '17-May-2017',


'Maintances' =&gt; [


    'repair_date' =&gt; '2017-05-17',


    'maintance_by' =&gt; 'pp',


    'maintance_purchase' =&gt; 'pp',


    'total_cost_invoked' =&gt; '555',


    'reference_bill' =&gt; '12',


    'notes' =&gt; 'jhgj',


],

];

$_COOKIE = [

'advanced-backend' =&gt; '48mrpiq93l1v46jn034m5jb6r7',


'_csrf-backend' =&gt; '745ef45fc325cd8c38c44a86e192568a82f26c64891c62511ef14376fec2fa3ba:2:{i:0;s:13:&quot;_csrf-backend&quot;;i:1;s:32:&quot;uXH-BrEtra5YUsK7xYYoaZ1JNoFfLSdR&quot;;}',


'_identity-backend' =&gt; '7d822e0a05a3d4434e9fae01aa9929ff64588b9558aeacb6d50f5e8a03503569a:2:{i:0;s:17:&quot;_identity-backend&quot;;i:1;s:46:&quot;[1,&quot;qjnhC35mEribFGRngZbbEEJXMxniN9Yh&quot;,2592000]&quot;;}',

];Integrity constraint violation – yii\db\IntegrityException

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (maintances.maintances, CONSTRAINT maintances_ibfk_1 FOREIGN KEY (asset_id) REFERENCES asset (id))

The SQL being executed was: INSERT INTO maintances (maintance_by, maintance_purchase, total_cost_invoked, reference_bill, notes) VALUES (‘pp’, ‘pp’, ‘555’, ‘12’, ‘jhgj’)

Error Info: Array

(

[0] =&gt; 23000


[1] =&gt; 1452


[2] =&gt; Cannot add or update a child row: a foreign key constraint fails (`maintances`.`maintances`, CONSTRAINT `maintances_ibfk_1` FOREIGN KEY (`asset_id`) REFERENCES `asset` (`id`))

)

Caused by: PDOException

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (maintances.maintances, CONSTRAINT maintances_ibfk_1 FOREIGN KEY (asset_id) REFERENCES asset (id))

in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Command.php at line 846

  1. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Schema.php at line 636

627628629630631632633634635636637638639640641642643644645

    &#036;exceptionClass = '&#092;yii&#092;db&#092;Exception';


    foreach (&#036;this-&gt;exceptionMap as &#036;error =&gt; &#036;class) {


        if (strpos(&#036;e-&gt;getMessage(), &#036;error) &#33;== false) {


            &#036;exceptionClass = &#036;class;


        }


    }


    &#036;message = &#036;e-&gt;getMessage()  . &quot;&#092;nThe SQL being executed was: &#036;rawSql&quot;;


    &#036;errorInfo = &#036;e instanceof &#092;PDOException ? &#036;e-&gt;errorInfo : null;


    return new &#036;exceptionClass(&#036;message, &#036;errorInfo, (int) &#036;e-&gt;getCode(), &#036;e);


}





/**


 * Returns a value indicating whether a SQL statement is for read purpose.


 * @param string &#036;sql the SQL statement


 * @return bool whether a SQL statement is for read purpose.


 */


public function isReadQuery(&#036;sql)


{
  1. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Command.php at line 856 – yii\db\Schema::convertException(PDOException, ‘INSERT INTO maintances (`maint…’)

  2. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\Schema.php at line 454 – yii\db\Command::execute()

  3. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\ActiveRecord.php at line 494 – yii\db\Schema::insert(‘maintances’, [‘maintance_by’ => ‘pp’, ‘maintance_purchase’ => ‘pp’, ‘total_cost_invoked’ => ‘555’, ‘reference_bill’ => ‘12’, …])

  4. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\ActiveRecord.php at line 461 – yii\db\ActiveRecord::insertInternal(null)

  5. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\db\BaseActiveRecord.php at line 635 – yii\db\ActiveRecord::insert(true, null)

  6. in C:\xxampp\htdocs\maintan\backend\controllers\MaintancesController.php at line 76 – yii\db\BaseActiveRecord::save()

70717273747576777879808182 if ($model->load(Yii::$app->request->post())) {

    //&#036;model-&gt;asset_id=&#036;get['asset_id'];








//print_r(&#036;model);


    &#036;model-&gt;save();





    return &#036;this-&gt;redirect(['view', 'id'=&gt; &#036;model-&gt;id]);





    } else {


        return &#036;this-&gt;render('create', [


            'model' =&gt; &#036;model,
  1. backend\controllers\MaintancesController::actionCreate()

  2. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\InlineAction.php at line 57 – call_user_func_array([backend\controllers\MaintancesController, ‘actionCreate’], [])

  3. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\Controller.php at line 156 – yii\base\InlineAction::runWithParams([‘r’ => ‘maintances/create’, ‘asset_id’ => ‘30’])

  4. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\Module.php at line 523 – yii\base\Controller::runAction(‘create’, [‘r’ => ‘maintances/create’, ‘asset_id’ => ‘30’])

  5. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\web\Application.php at line 102 – yii\base\Module::runAction(‘maintances/create’, [‘r’ => ‘maintances/create’, ‘asset_id’ => ‘30’])

  6. in C:\xxampp\htdocs\maintan\vendor\yiisoft\yii2\base\Application.php at line 380 – yii\web\Application::handleRequest(yii\web\Request)

  7. in C:\xxampp\htdocs\maintan\backend\web\index.php at line 17 – yii\base\Application::run()

11121314151617 require(DIR . ‘/../../common/config/main.php’),

require(__DIR__ . '/&#46;&#46;/&#46;&#46;/common/config/main-local.php'),


require(__DIR__ . '/&#46;&#46;/config/main.php'),


require(__DIR__ . '/&#46;&#46;/config/main-local.php')

);

(new yii\web\Application($config))->run();

$_GET = [

'r' =&gt; 'maintances/create',


'asset_id' =&gt; '30',

];

$_POST = [

'_csrf-backend' =&gt; 'bXBKX1lfM2YYKAJyGy12Eh8RfwYMLHhRFSkTMDgFAiwjHww5FQxXNA==',


'repair_date-maintances-repair_date-disp' =&gt; '17-May-2017',


'Maintances' =&gt; [


    'repair_date' =&gt; '2017-05-17',


    'maintance_by' =&gt; 'pp',


    'maintance_purchase' =&gt; 'pp',


    'total_cost_invoked' =&gt; '555',


    'reference_bill' =&gt; '12',


    'notes' =&gt; 'jhgj',


],

];

$_COOKIE = [

'advanced-backend' =&gt; '48mrpiq93l1v46jn034m5jb6r7',


'_csrf-backend' =&gt; '745ef45fc325cd8c38c44a86e192568a82f26c64891c62511ef14376fec2fa3ba:2:{i:0;s:13:&quot;_csrf-backend&quot;;i:1;s:32:&quot;uXH-BrEtra5YUsK7xYYoaZ1JNoFfLSdR&quot;;}',


'_identity-backend' =&gt; '7d822e0a05a3d4434e9fae01aa9929ff64588b9558aeacb6d50f5e8a03503569a:2:{i:0;s:17:&quot;_identity-backend&quot;;i:1;s:46:&quot;[1,&quot;qjnhC35mEribFGRngZbbEEJXMxniN9Yh&quot;,2592000]&quot;;}',

];[font="Arial"][/font]