Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add config for SQLite3 Foreign Keys #6050

Merged
merged 1 commit into from
May 30, 2022

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented May 30, 2022

Need to rebase after merging #6049

Description
Fixes #6008

  • add DB config foreignKeys for SQLite3 (disabled by default)

How to Test

<?php

namespace App\Controllers;

use Config\Database;

class Home extends BaseController
{
    public function index()
    {
        $group = [
            'database'    => 'sqlite3.db',
            'DBDriver'    => 'SQLite3',
            'DBPrefix'    => 'db_',
            'DBDebug'     => (ENVIRONMENT !== 'production'),
            'foreignKeys' => true, // Add the config
        ];
        $db = \Config\Database::connect($group);

        $this->forge = Database::forge($group);

        $this->createTableAuthUsers();
        $this->createTableAuthIdentities();

        $prefix = $db->getPrefix();

        $sql = "INSERT INTO {$prefix}auth_identities"
            . ' (user_id, type, secret, created_at)'
            . " VALUES (2, 'email_2fa', '479123', '2022-05-18 23:01:46')";
        $db->simpleQuery($sql);

        $query = $db->query("select * from {$prefix}auth_users");
        var_dump($query->getResult());
        $query = $db->query("select * from {$prefix}auth_identities");
        var_dump($query->getResult());
    }

    private function createTableAuthUsers()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'int',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'username' => [
                'type'       => 'varchar',
                'constraint' => 30,
                'null'       => true,
            ],
            'active' => [
                'type'       => 'tinyint',
                'constraint' => 1,
                'null'       => 0,
                'default'    => 0,
            ],
            'last_active' => [
                'type' => 'datetime',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'datetime',
                'null' => true,
            ],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey('username');
        $this->forge->createTable('auth_users', true);
    }

    private function createTableAuthIdentities()
    {
        $this->forge->addField([
            'id'           => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'user_id'      => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'type'         => ['type' => 'varchar', 'constraint' => 255],
            'secret'       => ['type' => 'varchar', 'constraint' => 255],
            'secret2'      => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
            'expires'      => ['type' => 'datetime', 'null' => true],
            'last_used_at' => ['type' => 'datetime', 'null' => true],
            'created_at'   => ['type' => 'datetime', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addUniqueKey(['type', 'secret']);
        $this->forge->addKey('user_id');
        $this->forge->addForeignKey('user_id', 'auth_users', 'id', '', 'CASCADE');
        $this->forge->createTable('auth_identities', true);
    }
}

Navigate to http://localhost:8080/

ErrorException
SQLite3::exec(): FOREIGN KEY constraint failed 

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label May 30, 2022
@kenjis kenjis force-pushed the fix-sqlite-foreign_keys branch from 9dcdf30 to 214f2d9 Compare May 30, 2022 11:00
@MGatner MGatner added the database Issues or pull requests that affect the database layer label May 30, 2022
@kenjis kenjis merged commit 4cf4f0f into codeigniter4:develop May 30, 2022
@kenjis kenjis deleted the fix-sqlite-foreign_keys branch May 30, 2022 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them database Issues or pull requests that affect the database layer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: SQLite3 foreign key does not work by default
3 participants