laracrud icon indicating copy to clipboard operation
laracrud copied to clipboard

Error during the migration generation

Open lavapi opened this issue 4 years ago • 9 comments

I am getting an error when trying to generate migration from DB.

vendor\digitaldream\laracrud\src\lara-crud\Crud\MigrationCrud.php:222 218| * @throws \Exception 219| */ 220| public function generateClassName($table) 221| {

222| $class = 'create' . ucfirst(camel_case($table)) . 'Table'; 223| 224| if (class_exists($class)) { 225| throw new \Exception('Migration for table ' . $table . ' already exists'); 226| }

Exception trace:

1 LaraCrud\Crud\MigrationCrud::generateClassName("users") vendor\digitaldream\laracrud\src\lara-crud\Crud\MigrationCrud.php:94

2 LaraCrud\Crud\MigrationCrud::template() vendor\digitaldream\laracrud\src\lara-crud\Crud\MigrationCrud.php:110

Please use the argument -v to see more details.

lavapi avatar Apr 14 '20 12:04 lavapi

Can you just replace camel_case($table) with Str::camel($table)

lavapi avatar Apr 14 '20 13:04 lavapi

Thanks for letting me know. Yes. i will definitely do that. I am working with V5 and its really cool. Until you have to wait.

digitaldreams avatar Apr 14 '20 13:04 digitaldreams

I hope you got the error. You already have a migration for your users table. Try something else and it should create migration methods for you.

digitaldreams avatar Apr 14 '20 13:04 digitaldreams

I fixed it temporarily but just informed you to fix it permanently :) P.S. perfect work...

lavapi avatar Apr 14 '20 13:04 lavapi

Hi, in addition to this error I discovered that you don't handle the UNSIGNED attribute in MySQL 8. The "unsigned" is returned with the column data type so it needs to be split first, otherwise the rules method won't match anything and returns nothing for the methodName which causes errors on line 71. Also, it seems to me that you only consider int and bigint for primary keys. Is there a reason for that?

hardkraft avatar Apr 28 '20 20:04 hardkraft

I made some changes starting on line 127

` $dataTypes = explode(' ',$column->type()); $dataType = $dataTypes[0]; $dataTypeAttr = !empty($dataTypes[1])? $dataTypes[1]:'';

        if ($column->isPk()) {
            if ($dataType == 'int') {
                $arr['methodName'] = 'increments';
            } elseif ($dataType == 'bigint') {
                $arr['methodName'] = 'bigIncrements';
            }
        } 
        if (empty($arr['methodName'])) {
            $arr['methodName'] = isset($this->columnMap[$dataType]) ? $this->columnMap[$dataType] : '';
        }`

I also added 'json' => 'json', to your $columnMap

Works well for my purposes but I didn't test it on anything else. Thanks for a great project :)

hardkraft avatar Apr 28 '20 21:04 hardkraft

@hardkraft thanks for code suggestion. I will definitely implement on next major version 5.0 which is under development now.

digitaldreams avatar Sep 03 '20 14:09 digitaldreams

I received this exact issue today in a fresh project, fresh "composer require digitaldream/laracrud --dev" - and line 222 had: $class = 'create' . ucfirst(camel_case($table)) . 'Table'; instead of $class = 'create'.Str::camel($table).'Table'; I swapped the lines out and it's fine now... I wish I could offer some insight but I'm a noob out of their depth. Probably something on this end. Otherwise it all looks very spiffy!

nicholasbieber avatar Aug 13 '21 13:08 nicholasbieber

Hi @nicholasbieber This issue is fixed on dev-master. You can use this version. The next Major version 5 is expected to be release in October this year.

digitaldreams avatar Aug 13 '21 15:08 digitaldreams