Welcome Guest, Not a member yet? Register   Sign In
Add unique key to existing column with Forge
#1

Hello,
How to add two column unique key to existing table? 
Best Regards,
ST
Reply
#2

(This post was last modified: 08-17-2022, 01:35 PM by captain-sensible. Edit Reason: wasn't paying attention he said via Forge so added last sentence )

lets look at an example of table creation :

Code:
CREATE TABLE "admin" (
    `Id`    INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
    `Name`    TEXT,
    `Password`    TEXT,
    `Role`    TEXT
)

In the above the id field is set to be an integer , its a primary key and unique. So entries might be 1,2,3 etc the entries are integers and also unique, there can only be
one 3, one 2 . Also the creation is set to autoincrement . This takes the headache to a degree of only having one instance, because if the last entry was 2, then the next one will be done to 3 ,without thinking.

Now the other thing is i can not have another field called id, i could have a field called ID, and have characteristic of unique also with integers . So i could have also 1.2 ,3
but to my understanding, there can only be one primary key.

So suppose you created a table but forgot to add a field called 'id' if it were a sqlite3 table my approach would be to use sqlitebrowser ( https://sqlitebrowser.org/) ; its an easy GUI and you would simply if on Linux get ot from your repo.


eg :

Code:
[andrew@darkstar:~][1]$ sudo pacman -Qi sqlitebrowser                     (08-17 21:28)
Name            : sqlitebrowser
Version         : 3.12.2-2
Description     : SQLite Database browser is a light GUI editor for SQLite databases,
                  built on top of Qt
im on Arch and got my via pacman . So its simply a case of opening a database , edit-> modify table using perhaps "execute sql" or manually. if you have a MySQl or Maria then phpMyAdmin from memory same approach




Now i just typed all that , then looked at the title WITH Forge - so i guess its not going to be much help. But you know what i will leave it for
those new to databases
CMS CI4     I use Arch Linux by the way 

Reply
#3

Code:
$this->forge->addField([
            'id' => [
                'type' => 'VARCHAR',
                'constraint' => 100,
                'null' => false,
            ],
            'account_id' => [
                'type' => 'INT',
                'constraint' => 10,
                'unsigned' => true,
            ],
            'code' => [
                'type' => 'VARCHAR',
                'constraint' => 150
            ],
            'name' => [
                'type' => 'VARCHAR',
                'constraint' => 250
            ],
            'created_at' => [
                'type' => 'DATETIME'
            ],
            'updated_at' => [
                'type' => 'TIMESTAMP'
            ]
        ])
            ->addPrimaryKey('id')
            ->addForeignKey('account_id', 'core_accounts', 'id', 'CASCADE', 'CASCADE')
            ->createTable('attribute_group', true);

Now in this instance primary key is added. need to update this table to add unique key here is scenario 'code' field need to be unique based on 'account_id'. A unique key 'account_id_code' is required here.
Best Regards,
ST
Reply




Theme © iAndrew 2016 - Forum software by © MyBB