Yii seems to neglect AUTO_INREMENT for PrimaryKey

I am not sure where this is coming from, I have dumped a copy of your query as json look at the column mapping the id which i believe is your primary key is being set to 3, find out where that is coming from and you have your problem solved.


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

The SQL being executed was: INSERT INTO `person` (`angelegt_von`, `angelegt_am`, `aktualisiert_am`, `id_person_art`, `id_person_mitarbeiter`, `id_personentyp_dominant`, `id_personentyp_peripher`, `id_personentyp_defizit`, `id_familienstand`, `id_anrede`, `id_titel`, `vorname`, `nachname`, `anrede_persoenlich`, `anrede_duzen`, `geburtsdatum`, `geburtsort`, `geburtsland`, `bemerkung_intern`, `schwerbehindert`, `einschraenkungen`, `aktiv`, `zuletzt_deaktiviert_am`, `aktualisiert_von`, `id`) VALUES (2, NOW(), NOW(), 2, NULL, NULL, NULL, NULL, 4, 1, NULL, 'llll', 'oooo', 1, 1, NULL, '', '', '', 1, '', 1, NULL, 2, 3)[/b]




// INSERT INTO person

// Integrity constraint violation: 1062 Duplicate entry '3' for key 'PRIMARY'

{

"angelegt_von": 2,

"angelegt_am": NOW(),

"aktualisiert_am": NOW(),

"id_person_art": 2,

"id_person_mitarbeiter": NULL,

"id_personentyp_dominant": NULL,

"id_personentyp_peripher": NULL,

"id_personentyp_defizit": NULL,

"id_familienstand": 4,

"id_anrede": 1,

"id_titel": NULL,

"vorname": 'llll',

"nachname": 'oooo',

"anrede_persoenlich": 1,

"anrede_duzen": 1,

"geburtsdatum": NULL,

"geburtsort": '',

"geburtsland": '',

"bemerkung_intern": '',

"schwerbehindert": 1,

"einschraenkungen": '',

"aktiv": 1,

"zuletzt_deaktiviert_am": NULL,

"aktualisiert_von": 2,

"id": 3,

}




why are you manually increment the keys,


 $model_person_ = Person::find();

        $id_max_person = $model_person_->select('id')->max('id');

        $id_max_person++; // this should not be here database will increment it for you

.

.

.


        $model_person->id = $id_max_person; 

                var_dump($model_person->id);

                die();

                $model_person->save();

                var_dump($model_person->id);

                die();

This is just a test!! Normally,database should do this for me. But following orders will have absolutely no impact


$model_person->id = $id_max_person;

This means:


   		var_dump($model_person->id);

will show up int 8,but it will be written another id into database…

I have abolutely no idea,where this value comes from. As U can see in my code(at page 1),I have nowhere defined writing id into database. Id should be written behind the scences,’ cause it is set AUTO_INCREMENT. I posted formular and Controller in this thread. Model has been created using Gii. I absolutely don’t understand, why I get confusing valiues for primary key…

Read this

https://stackoverflow.com/questions/8923114/how-to-reset-auto-increment-in-mysql

I know about this. I used to set AUTO_INCREMENT Index back in another project. That’s not problem,here. Problem here is following:


 	var_dump($model_person->id);

will show up


int 8

as it should be. But


   	$model_person->save();

                var_dump($model_person->id);

will show up


int 64960 

So,method save() seems to change id. I have no idea,why!

[b]

[/b]

Maybe, this behavior is 'caused by model using uuid-behavior of mootensai? If I delete this Behavior, I will get error like this





[b]Database Exception – [url="http://www.yiiframework.com/doc-2.0/yii-db-exception.html"]yii\db\Exception[/url][/b]

                

[b]SQLSTATE[22003]: Numeric value out of range: 167 Out of range value for column 'id' at row 1

The SQL being executed was: INSERT INTO `bewerber` (`angelegt_von`, `id_person`, `id_person_rekrutiert_von`, `id_abrechnungsweg`, `id_kanal`, `id_bewerberquelle`, `id_ba_xml_gelernter_beruf_1`, `id_ba_xml_gelernter_beruf_2`, `id_ba_xml_gelernter_beruf_3`, `umkreis`, `arbeitsuchend_seit`, `avgs`, `arbeitsumfang_vollzeit`, `avgs_betrag`, `ablaufdatum_avgs`, `wunschgehalt_brutto`, `beurteilung_fachlich`, `beurteilung_persoenlich`, `sonstiges`, `verfuegbar_ab`, `gesuchte_positionen`, `anschreiben`, `arbeitsumfang_teilzeit`, `arbeitszeit_bueroueblich`, `arbeitszeit_vormittag`, `arbeitszeit_nachmittag`, `arbeitszeit_abend`, `arbeitszeit_nacht`, `arbeitszeit_wochenende`, `schichtbereitschaft`, `wochenstunden_minimum`, `wochenstunden_maximum`, `bemerkung_intern`, `quereinsteiger`, `zeitarbeit`, `pkw`, `fuehrerschein_pkw`, `fuehrerschein_lkw`, `fuehrerschein_omnibus`, `reisebereitschaft`, `kandidat`, `kandidat_seit`, `veroeffentlichen_bewerberboerse`, `kontakt_halten`, `aktiv`, `zuletzt_deaktiviert_am`, `angelegt_am`, `optimistic_lock`, `aktualisiert_am`) VALUES (5, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 10, NULL, 1, 1, 2000, NULL, 750, '', '', '', NULL, '', '', NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, 10, 30, '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, 1, 1, 1, NULL, NOW(), 0, NOW())[/b]

        Error Info: Array(    [0] => 22003    [1] => 167    [2] => Out of range value for column 'id' at row 1



Here is UUID Behavior of mootensai in model:





    public function behaviors() {

        return [

            'timestamp' => [

                'class' => TimestampBehavior::className(),

                'createdAtAttribute' => 'angelegt_am',

                'updatedAtAttribute' => 'aktualisiert_am',

                'value' => new \yii\db\Expression('NOW()'),

            ],

            'uuid' => [

                'class' => UUIDBehavior::className(),

                'column' => 'id',

            ],

        ];

    }



I’m pretty sure that the problem lies in the uuid-behavior of mootensai, or, rather precisely, in the wrong usage of it. Why do you have to apply uuid-behavior to ‘id’?

And have you created the models and their CRUDs using mootensai’s enhanced gii?

I’d like to recommend you to remove all the extensions of mootensai and restart the project from the scratch without them. It’s not a good practice to use such third-party extensions that you are not very sure what they are doing.

Or, read the manuals of yii and the extensions to understand them, and debug your code.

[P.S.]

I don’t want to say anything about moontensai’s extensions. I don’t know if they are good or not.




               $postdata = array(

                    'dogpic' => $dogpic,

                    'dogname' => $dogname,

                    'sex' => $sex,

                    'comments' => $comments,

                    'adopted' => $adopted,

                    'lastedit' => $lastedit

                );

               DB::table('dogs')->insert($postdata);



No id used for insert

Now an edit / update




                $postdata = array(

                    'dogid' => $dogid,

                    'dogpic' => $dogpic,

                    'dogname' => $dogname,

                    'sex' => $sex,

                    'comments' => $comments,

                    'adopted' => $adopted,

                    'lastedit' => $lastedit

                );

                 DB::table('dogs')

                        ->where('dogid', $dogid)

                        ->update($postdata);



An "id" this case dogid used in an update passed hidden.

But really this is sql 101.

Example in laravel, I don’t have such a Yii2 example yet.

I don’t think there is much to this to understand, just the way it is.

And the title:

A framework doesn’t have anything to do with that, that would be MySql or whatever database you are using.

everybody and their grandmother is creating extensions these days without good docs confusing the users, as softark said remove the extensions that could be where the id is being generated perhaps try commenting it out

I strongly recommend setting up xdebug and perform line by line execution between two mentioned var_dumps. And you will see where id gets written.

Here’s the quick guide of what I am using: https://wiki.eclipse.org/Debugging_using_XDebug

[list=1][*]I can’t debug yii projects line by line,'cause there are dependencies of classes,which will throw out errors like this:


Fatal error:  Class 'yii\web\Controller' not found in E:\xampp\htdocs\yii2_perswitch\frontend\modules\kontakt\controllers\KontaktAdresseController.php on line [18

[]I just can debug whole project, but that’s not targeted getting value of id[]I decided to remove mootensai extension. I hope - it’s just anesperance - error will be removed[/list]

Yes! CRUDS as well as models have been generated using extenden Gii of mootensai. There are about 108 tables in database and all these models have same behaviour…

I already tried to do this. If I comment out mootensai extension, I will get error having [color="#606060"][size="2"]Posted [/size][/color]Yesterday, 11:26 PM

[size="2"]I will try generate models again without this extension![/size]

I would recommend phpdbg if you are running php v5.4, I have been using it for almost 2 years now its awesome lightweight and pretty good feels like good gdb. It has been the default php debugger since v5.4.

I use XDebug.

What’s difference between phpdbg and xDebug, or better:

It’s really possible, debugging framework projects with phpdbg? Can I use phpdbg with Netbeans,too? Is there any plugin or tutorial available, using phhdbg?

Thx for ur hint!

P.S.:I am using php 7.X

P.S.:II After debugging my project with XDebug setting breakpoint in Controller I noticed, that id had value like this:


"91ba711bbd7911e7a5a8002522a0732a"

in BasActiveRecord.php. I suppose, that’s responsible for confusing values for PrimayKey.What do you think?

I guess it’s an UUID created by the UuidBehavior.

I think, too, it is. But why and how this UUID can be responsible for confusing values for PrimaryKey? Why should mootensai develop and publish a behaviour class like this?

Unfortunately,I can’t find informations about this class in web. There’s just a GitHub souce here, without any further informations.

Model in project were not created by me, but by another programmer. I usually don’t use exensions, which I don’t know, what they do.

So, as it seems, we have to reimplement 108 models. Should we, in order to get problem solved?? Or could it be possible, using this class in a right way, so that PrimaryKey values will be as they should be. Is there anybody out there, who has experiences with this UUID extension?

An ordinary integer is a 32-bit number, and even a long integer (BIGINT in MySql) is a 64-bit number :rolleyes: .

And this is UUID() function of MySql

https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_uuid

Note that the return value of UUID() is a string.

And the extension tries to set this lengthy string to the target column of the model before saving.

That’s why you got “91ba711bbd7911e7a5a8002522a0732a” for “id”. Probably it will be parsed as an integer and result in the value of 91. This explains very well why you often get duplicated values for “id” ;)

I don’t know about that extension, but Yii extensions are not screened. Anyone can release any extension, there is no guarantee, no single source of truth.

That’s usually a good thing, but sometimes it can get you into trouble.

For this reason, I think twice before I rely on an extension not written by myself… ;)

[size="2"]

Bingo.That’s it

After having relinquished using UUID-class of mootensai in both models everything is gonna all right by now.I absolutely don’t know, why another menber of my team created models using this fuckin bullshit class, which wasted me and U time and nerves.[/size]

[size=“2”]So, I will approach my chief ordering my team member deleting this class in 108 models.I’m convinced, my team member didn’t know what UUID is about, no less than about this class of mootensai.

I take a call for everybody who tried to help me giving reputation points. Wihout U, I never wolud have got problem solved.Keep it up!

P.S.: I use a lot of extension of kartik. Usually,these widgets should do its jobs without problems. Any worth experiences with widgets of kartik experienced?[/size]

Kartik is rock solid!

Hello. For someone with the same issue:

Bye!