CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Bug: Sqlite drops foreign keys when altering table

Open sclubricants opened this issue 3 years ago • 0 comments

PHP Version

8.0

CodeIgniter4 Version

4.3

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

SQLite3

What happened?

When performing certain table modifications foreign keys are dropped. This happens because Sqlite uses a Table class to recreate the table when modified. This Table class is missing logic to recreate foreign keys.

Steps to Reproduce

Test Sqlite with the following:

    public function testDropColumnForeignKey()
    {
        $this->forge->dropTable('forge_test_invoices', true);
        $this->forge->dropTable('forge_test_users', true);

        $attributes = [];

        $this->forge->addField([
            'id' => [
                'type'       => 'INTEGER',
                'constraint' => 11,
            ],
            'second_id' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
            ],
        ]);
        $this->forge->addPrimaryKey(['id', 'second_id']);
        $this->forge->createTable('forge_test_users', true, $attributes);

        $fields = [
            'id' => [
                'type'       => 'INTEGER',
                'constraint' => 11,
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
            ],
            'users_id' => [
                'type'       => 'INTEGER',
                'constraint' => 11,
            ],
            'users_second_id' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'preferences' => ['type' => 'TEXT'],
        ];

        $this->forge->addField($fields);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey(['users_id', 'users_second_id'], 'forge_test_users', ['id', 'second_id'], 'CASCADE', 'CASCADE');

        $this->forge->createTable('forge_test_invoices', true, $attributes);

        $foreignKeyData = $this->db->getForeignKeyData('forge_test_invoices');

        $this->assertCount(1, $foreignKeyData);

        $this->forge->dropColumn('forge_test_invoices', 'preferences');

        $foreignKeyData = $this->db->getForeignKeyData('forge_test_invoices');

        $this->assertCount(1, $foreignKeyData);

        $this->forge->dropTable('forge_test_invoices', true);
        $this->forge->dropTable('forge_test_users', true);
    }

Expected Output

Expected success

Anything else?

No response

sclubricants avatar Aug 29 '22 22:08 sclubricants