yiisoft / yii2-gii

Yii 2 Gii Extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
202 stars 192 forks source link

The table does not exist: uacademy_persona. Model generator throws an error. #524

Closed glodov closed 1 year ago

glodov commented 1 year ago

What steps will reproduce the problem?

I have created a model for the Persona use (an extended version of the profile) with a more complicated structure of data. I have described a model according to insolita/yii2-migration-generator@~3.1:

<?php

/**
 * Model class for Persona "uacademy_persona"
 */
namespace yaro\humhub\modules\UAcademy\models;

use humhub\components\ActiveRecord;
use humhub\modules\user\authclient\AuthClientHelpers;
use Yii;

/**
 * Model class for Persona "uacademy_persona"
 *
 * @property integer $id                @column pk|comment("Id")
 * @property integer $user_id           @column integer()|notNull
 * @property string  $name_first        @column string(60)|null
 * @property string  $name_last         @column string(60)|null
 * @property string  $name_parent       @column string(60)|null
 * @property string  $description       @column string(160)|null
 * @property string  $text              @column text(9000)|null
 * @property string  $rank_academic     @column string(60)|null
 * @property string  $rank_science      @column string(60)|null
 * @property string  $birth_place       @column string(60)|null
 * @property string  $birth_date        @column datetime()|null
 * @property string  $science_interests @column object()|null
 * @property array   $degrees           @column object()|null
 * @property array   $publications      @column object()|null
 * @property array   $activities        @column object()|null
 * @property array   $member_of         @column object()|null
 * @property array   $awards            @column object()|null
 * @property array   $sources           @column object()|null
 * @property array   $contacts          @column object()|null
 * @property string  $source_link       @column object()|null
 * @property \humhub\modules\user\models\User $user
 */
class Persona extends ActiveRecord
{
    /**
     * @since 1.3.2
     */
    const SCENARIO_EDIT_ADMIN = 'editAdmin';
    const SCENARIO_REGISTRATION = 'registration';
    const SCENARIO_EDIT_PROFILE = 'editProfile';

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'uacademy_persona';
    }
// ...
}

I tried to run a table generator and it throws me an error: Table does not exist. Ok. I have created a table in the database with MariaDB commands in the MariaDB client, so what I have in the database:

MariaDB [intranet]> describe uacademy_persona;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.004 sec)

But I had the same error again. So I have run clear cache command in the console:

php yii cache cache/flush-all  
The following caches were found in the system:

    * cache (yii\caching\FileCache)

yii cache cache/flush-schema
The following caches were found in the system:

    * cache (yii\caching\FileCache)php 

But this did not help either.

What's expected?

Migration file was created to run the migration as it should work.

What do you get instead?

An error "The table does not exist: uacademy_persona"

Additional info

Q A
Yii version 2.0.47
PHP version 8.1.11
Database version 10.8.3-MariaDB Homebrew
Operating system Darwin Kernel Version 21.5.0
bizley commented 1 year ago

Make sure your application has access to this database and the table.

glodov commented 1 year ago

Make sure your application has access to this database and the table.

Of course. It is.

cat config/dynamic.php 
<?php return [
    'components' => [
        'db' => [
            'class' => 'yii\\db\\Connection',
            'dsn' => 'mysql:host=localhost;dbname=intranet',
            'username' => 'root',
            'password' => 'root',
        ],
...

Entering and checking with the same user with MySQL (MariaDB) console.

mysql -u root -p

MariaDB [(none)]> use intranet;
Database changed
MariaDB [intranet]> show tables like "uacademy_%";

+---------------------------------+
| Tables_in_intranet (uacademy_%) |
+---------------------------------+
| uacademy_persona                |
+---------------------------------+
1 row in set (0.001 sec)

MariaDB [intranet]> select * from uacademy_persona;
Empty set (0.002 sec)

MariaDB [intranet]> insert into uacademy_persona values ();
Query OK, 1 row affected (0.003 sec)

MariaDB [intranet]> select * from uacademy_persona;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.000 sec)

It seems framework is trying to get data from the table (even with the generator) before creating it.

schmunk42 commented 1 year ago

Do you have other tables referencing uacademy_persona by FK?

AFAIR gii reads all tables and sometimes mixes up table names when using ie. a table_prefix.

bizley commented 1 year ago

So far I don't see any proof that the app has access to the database, you are showing only the db console.

glodov commented 1 year ago

Make sure your application has access to this database and the table.

And today it works. After such manipulation of inserting data into the table. I think it's some kind of cache, and it would be perfect to understand which one.

Now I have removed the table and the migration generator works.

Do you have other tables referencing uacademy_persona by FK?

AFAIR gii reads all tables and sometimes mixes up table names when using ie. a table_prefix.

Nope. Works well now.

I don't understand why it did not work yesterday.

glodov commented 1 year ago

So far I don't see any proof that the app has access to the database, you are showing only the db console.

How can I make a proof for you otherwise than a console (good to know for future reporting)?

bizley commented 1 year ago

By running some SQL query using the app. Anyway, interesting, maybe indeed DB schema cache is the reason, do you have it switched on?

glodov commented 1 year ago

By running some SQL query using the app. Anyway, interesting, maybe indeed DB schema cache is the reason, do you have it switched on?

I though so, and so I used the command from the terminal to clear the cache, but it did not help.

My config for the cache is:

<?php return [
    'components' => [
        'cache' => [
            'class' => 'yii\\caching\\FileCache',
            'keyPrefix' => 'humhub',
        ],
// ...

and now I have another issue with the popuping up the preview of the file. I've recorded a screen with the explanation, it is available here: https://youtu.be/86Lc_MJ9jKQ. I have my dark theme enabled (external CSS for the generator) but even with the disabled dark theme popup is not visible.

bizley commented 1 year ago

Is it possible that some Humhub component is breaking that? I'm not familiar with Humhub so I'm afraid I won't be able to help. Please double check the browser console for any network and JS errors as well.

glodov commented 1 year ago

Is it possible that some Humhub component is breaking that? I'm not familiar with Humhub so I'm afraid I won't be able to help. Please double check the browser console for any network and JS errors as well.

No errors. The response is very correct with the preview of the file, and more interesting, there is the correct HTML code generated and almost visible on the highest level with z-index, but it is transparent.

If you have time (5 minutes) to check, I've recorded another video with the research on this transparency error. Here is the video: https://youtu.be/ing9zx4bDYU.

My experience tells me it is not related to HumHub, it is pure Yii2 generator run in the browser as a part of HumHub platform.

glodov commented 1 year ago

I found an issue. It's an opacity of the .fade class for the modal dialog. I shall try to make a pull request with the fix.

glodov commented 1 year ago

And again, I have the same issue after I removed the table. It makes no sense to require an existent table in the generator module. How can migrations be created then?

bizley commented 1 year ago

What do you mean? Is there a table or is there not? Do you want to generate a model based on it or a migration?

glodov commented 1 year ago

What do you mean? Is there a table or is there not? Do you want to generate a model based on it or a migration?

No table. When I created it manually it worked without any error, but migration did not run. Nothing new to migrate.

When I removed the table, it continues to show an error on the step of preview or generation of migration file.

It makes no sense to create a table manually in the database, this is why I use a migration generator to generate it without errors.

I am going to dig into the architecture of the migration process and try to fix it.

glodov commented 1 year ago

What do you mean? Is there a table or is there not? Do you want to generate a model based on it or a migration?

When the checkbox "Only by phpdoc annotation" is checked there is no error. When it's unchecked generator throws an error. Should a message or hint be added here for this checkbox?

I've spent quite some time figuring this out.

Only by phpdoc annotation

And now, I have an issue with the "object" column type. And the yii\db\SchemaBuilderTrait does not have such a function object() but it has json() which is not described in phpdoc annotation. But it works with "json" in the way to create "longtext" in MariaDB.

What would you suggest to improve @bizley to help developers to understand this builder with no errors and with the correct json format?

bizley commented 1 year ago

We are talking here about several things at the same time so it would be nice to extract a single thing into a separate issue if you find something not working properly. As for the builder documentation I'm afraid I cannot help you with that since Gii docs are quite minimal, it would be nice to have it completed.

glodov commented 1 year ago

Thanks.

This question is already part of the migration generator module, so it's issued here https://github.com/Insolita/yii2-migrik/issues/49