codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: Migration Problem "Incorrect table definition; there can be only one auto column and it must be defined as a key" #7477

Closed officialxviid closed 1 year ago

officialxviid commented 1 year ago

PHP Version

8.1

CodeIgniter4 Version

^4.0

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MySQL

What happened?

What I experienced: 1) Unable to migrate database. 2) In the 'id' field, I can't customize a value.

For point 2: For point 2: I looked at 'CodeIgniter\Database\Forge' at line 362-367, there is an array containing:

'id' => [
   'type'           => 'INT',
   'constraint'     => 9,
   'auto_increment' => true,
],

Can I not customize the 'id' field value in the table?

Steps to Reproduce

1) Config File

I created 1 config file in app/Config/ExampleConfig.php 

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class ExampleConfig extends BaseConfig
{
    /**
     * Fields of table
     *
     * @var array
     */
    public array $fields = [
        'id'            => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
        'created_at'    => ['type' => 'datetime', 'null' => true],
        'updated_at'    => ['type' => 'datetime', 'null' => true],
        'deleted_at'    => ['type' => 'datetime', 'null' => true],
        'code'          => ['type' => 'varchar', 'constraint' => 255],
        'symbol'        => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
        'name'          => ['type' => 'varchar', 'constraint' => 255],
    ];

    /**
     * Foreign Key
     * ---
     * 
     * Example:
     * $foreignKey = [
     *  [
     *      'fieldName'     => 'example_id',
     *      'tableName'     => 'example_table',
     *      'tableField'    => 'id',
     *      'onUpdate'      => '',
     *      'onDelete'      => 'CASCADE',
     *      'fkName'        => '',
     *  ],
     * ]
     *
     * @var array
     */
    public array $foreignKey = [];
}

2) Migration File

I created 1 migration file in app/Database/Migrations/2023-05-01-231938_CreateTableExample.php 

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;
use Config\ExampleConfig;

class CreateTableExample extends Migration
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->config = new ExampleConfig();
        $this->DBGroup = $this->config->DBGroup;
    }

    public function up()
    {
        if (count($this->config->fields) > 0 && $this->config->table) {
            // Fields
            $this->forge->addField($this->config->fields);

            // Create Table
            $this->forge->createTable($this->config->table, true);
        }
    }

    public function down()
    {
        if ($this->config->table) {
            // drop constraints first to prevent errors
            if ($this->db->DBDriver !== 'SQLite3') { // @phpstan-ignore-line
                // Foreign Key
                foreach ($this->config->foreignKey as $foreignKey) {
                    if ($foreignKey['fieldName']) {
                        $this->forge->dropForeignKey($this->config->table, $this->config->table . '_' . $foreignKey['fieldName'] . '_foreign');
                    }
                }
            }

            // Drop Table
            $this->forge->dropTable($this->config->table, true);
        }
    }
}

3) Run Migration

php spark migrate

Expected Output

It is possible to customize a field in the config file 🤔

Anything else?

I instead get an error message:

[CodeIgniter\Database\Exceptions\DatabaseException]

Incorrect table definition; there can be only one auto column and it must be defined as a key

at SYSTEMPATH\Database\BaseConnection.php:646
kenjis commented 1 year ago

^4.0 is not a exact version. It shows version range. Please write the exact version you use.

kenjis commented 1 year ago

Can I not customize the 'id' field value in the table?

Yes, you can. The code you pointed is for: https://codeigniter4.github.io/CodeIgniter4/dbmgmt/forge.html#creating-an-id-field

Do like this: https://github.com/codeigniter4/shield/blob/1225c80ac08ede8d97f9bdf661e80dee6d83515a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php#L30-L43

kenjis commented 1 year ago

We use GitHub issues to track BUGS and to track approved DEVELOPMENT work packages. We use our forum to provide SUPPORT and to discuss FEATURE REQUESTS.

officialxviid commented 1 year ago

Thanks sir.. My Allah bless u

officialxviid commented 1 year ago

Thanks sir.. My Allah bless u

Sorry for the wrong place to ask