Yii2 won't recognize primary key

I’m using a MariaDB database (with InnodDB engine) and for some reason Yii complains about some tables not having a primary key. The tables have a primary key defined in database (with an unsigned INT auto increment value), but Yii throws the following database exception:

app\\models\\Answer does not have a primary key. You should either define a primary key for the corresponding table or override the primaryKey() method.

If I add the primaryKey() method manually to my model it will work then, but it causes errors at some other places, for example in this case.

Why doesn’t Yii recognize my primary keys automatically? I’m using the latest version of Yii2.

Can you post table DDL?

Here you are:


  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `guid` bigint(20) AS (CONCAT(73, audit_id, 919191, question_id)) PERSISTENT,

  `guid_old` int(10) unsigned DEFAULT NULL,

  `ref` varchar(128) NOT NULL DEFAULT '',

  `name` varchar(255) NOT NULL DEFAULT '',

  `depth` tinyint(4) DEFAULT NULL,

  `description` text,

  `audit_id` int(10) unsigned DEFAULT NULL,

  `question_id` int(10) unsigned DEFAULT NULL,

  `template_ids` varchar(128) DEFAULT NULL,

  `user_id` int(10) unsigned DEFAULT NULL,

  `created_at` datetime DEFAULT NULL,

  `updated_at` datetime DEFAULT NULL,

  `na` tinyint(1) DEFAULT NULL,

  `passed` tinyint(1) DEFAULT NULL,

  `score` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`),

  KEY `FK_answer_audit` (`audit_id`),

  KEY `FK_answer_question` (`question_id`),

  KEY `FK_answer_user` (`user_id`),

  KEY `guid` (`guid`)


Was your model generated with Gii? When exactly Yii throws this exception?

Yes, the model was generated with Gii.

It’s quite random, sometimes it throws this exception and sometimes it doesn’t. I don’t really know what it depends on.

Do you use any caching? How about schema cache?

Yes, I use schema caching, but whenever I experience the problem I flush all caches including schema cache, but the problem won’t go away.