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

Multiple primary key defined #13

Open
ratacibernetica opened this issue Sep 25, 2015 · 3 comments
Open

Multiple primary key defined #13

ratacibernetica opened this issue Sep 25, 2015 · 3 comments

Comments

@ratacibernetica
Copy link

I had a couple tables with 2 primary keys in my Schema (was using dektrium's yii2-user).

This is the code that generates the error:

 /* MYSQL */
        if (!in_array('auth_assignment', $tables)) {
            if ($dbType == "mysql") {
                $this->createTable('{{%auth_assignment}}', [
                    'item_name' => 'VARCHAR(64) NOT NULL',
                    0 => 'PRIMARY KEY (`item_name`)',
                    'user_id' => 'VARCHAR(64) NOT NULL',
                    1 => 'PRIMARY KEY (`user_id`)',
                    'created_at' => 'INT(11) NULL',
                        ], $tableOptions_mysql);
            }
        }

Solved it by changing it to:

/* MYSQL */
        if (!in_array('auth_assignment', $tables)) {
            if ($dbType == "mysql") {
                $this->createTable('{{%auth_assignment}}', [
                    'item_name' => 'VARCHAR(64) NOT NULL',
                    'user_id' => 'VARCHAR(64) NOT NULL',
                    'created_at' => 'INT(11) NULL',
                        ], $tableOptions_mysql);
                $this->addPrimaryKey(
                                     'auth_assignment_pk',
                                    'auth_assignment',
                                    ['item_name','user_id']
                          );
            }
        }

Other than that, everything worked fine!

Great plugin, thanks!

@c006
Copy link
Owner

c006 commented Sep 25, 2015

Thanks, glad it works for you.

I appreciate the code.
Hadn't tested for multiple PK's.

I'll put that on the list to do.

Regards,
Jon

On Thu, Sep 24, 2015 at 8:20 PM, Ratacibernetica [email protected]
wrote:

I had a couple tables with 2 primary keys in my Schema (was using
dektrium's yii2-user).

This is the code that generates the error:

/* MYSQL */ if (!in_array('auth_assignment', $tables)) { if ($dbType == "mysql") { $this->createTable('{{%auth_assignment}}', [ 'item_name' => 'VARCHAR(64) NOT NULL', 0 => 'PRIMARY KEY (item_name)', 'user_id' => 'VARCHAR(64) NOT NULL', 1 => 'PRIMARY KEY (user_id)', 'created_at' => 'INT(11) NULL', ], $tableOptions_mysql); } }

Solved it by changing it to:

/* MYSQL */ if (!in_array('auth_assignment', $tables)) { if ($dbType == "mysql") { $this->createTable('{{%auth_assignment}}', [ 'item_name' => 'VARCHAR(64) NOT NULL', 'user_id' => 'VARCHAR(64) NOT NULL', 'created_at' => 'INT(11) NULL', ], $tableOptions_mysql); $this->addPrimaryKey('auth_assignment_pk','auth_assignment',['item_name','user_id']); } }

Other than that, everything worked fine!

Great plugin, thanks!


Reply to this email directly or view it on GitHub
#13.

@c006
Copy link
Owner

c006 commented Dec 9, 2015

Hi.

Thank you for the code, I will add it.
I had not thought of composite keys.

Regards,
Jon

On Wed, Dec 9, 2015 at 4:51 AM, Sedov Sergey [email protected]
wrote:

or change source in DefaultController.php in line 90

$k = 0; foreach ($columns->columns as $column) { $appUtility = new AppUtility($column, $dbType); $output->addStr($appUtility->string . "',"); if ($column->isPrimaryKey) { $output->addStr($k . " => 'PRIMARY KEY (" . $column->name . ")',"); } $k++; }

to

$k = 0; $pk = false; foreach ($columns->columns as $column) { $appUtility = new AppUtility($column, $dbType); $output->addStr($appUtility->string . "',"); if ($column->isPrimaryKey && !$pk) { $output->addStr($k . " => 'PRIMARY KEY (" . $column->name . ")',"); $pk = true; } elseif ($column->isPrimaryKey && $pk) $output->addStr($k . " => 'KEY (" . $column->name . ")',"); $k++; }


Reply to this email directly or view it on GitHub
#13 (comment)
.

@c006 c006 closed this as completed Dec 10, 2015
@antick
Copy link

antick commented Jul 23, 2017

I have encountered the same issue. I think you closed this but haven't fixed the code yet.

@c006 c006 reopened this Jul 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants