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

Error in migration createTable (and possible other) method when DB data type has more than one word #19759

Closed
SOHELAHMED7 opened this issue Feb 8, 2023 · 4 comments
Labels
Milestone

Comments

@SOHELAHMED7
Copy link
Contributor

double precision is the SQL data type. It has two words.

Consider method \yii\db\migration::createTable()

What steps will reproduce the problem?

$this->createTable('{{%fruits}}', [
            'dp' => 'double precision NULL DEFAULT null',

What is the expected result?

This should create table in DB successfully.

What do you get instead?

I get SQL error: Part of SQL created was

dp double precision precision NULL DEFAULT null

'double precision precision' creates SQL syntax error.

Solution

2 Possibilities

  • implement solution to this error
  • change the docs for createTable() method and similar methods as mentioned below:
* where name stands for a column name which will be properly quoted by the method, and definition
-      * stands for the column type which can contain an abstract DB type.
+      * stands for the column type which must contain an abstract DB type. 

Additional info

Q A
Yii version 2.0.*
PHP version
Operating system
@samdark samdark added the type:bug Bug label Feb 9, 2023
@samdark
Copy link
Member

samdark commented Feb 9, 2023

What would you prefer?

@SOHELAHMED7
Copy link
Contributor Author

SOHELAHMED7 commented Feb 9, 2023

I prefer to implement the solution but there are two ways createTable() can be used:

class m200000_000000_create_table_fruits extends \yii\db\Migration
{
    public function safeUp()
    {
        $this->createTable('{{%fruits}}', [
            'ts' => $this->timestamp()->null()->defaultExpression("(CURRENT_TIMESTAMP)"),
            'ts2' => $this->timestamp()->null()->defaultValue("2011-11-11 00:00:00"),
            'ts3' => $this->timestamp()->null()->defaultValue("2022-11-11 00:00:00"),           
            0 => '"ts4" timestamp NULL DEFAULT \'2022-11-11 00:00:00\'',
            1 => '"ts5" timestamp NULL DEFAULT (CURRENT_TIMESTAMP)',
            2 => '"ts6" timestamp NULL DEFAULT \'2000-11-11 00:00:00\'',
            3 => '"d" date NULL DEFAULT (CURRENT_DATE + INTERVAL \'1 YEAR\')',
            4 => '"d2" text NULL DEFAULT (CURRENT_DATE + INTERVAL \'1 YEAR\')',
            5 => '"d3" text NULL DEFAULT \'text default\'',

           'c7' => 'double precision null default null', <---- this will lead to error
           6 => 'c8 double precision null default null', <---- this will not lead to error

            'ts7' => $this->date()->null()->defaultExpression("(CURRENT_DATE + INTERVAL '1 YEAR')"),
        ]);
    }

so the solution for this bug is to use numeric index (or ignore index) and define column defination as shown above if physical data type needs to be used in migration.

so at least a work-around is present for this bug. so at this moment the implementation is not needed and to conclude I prefer to just change the docs and add an example in the docs to use both ways

@samdark
Copy link
Member

samdark commented Feb 9, 2023

Alright. Would you help updating docs?

@SOHELAHMED7
Copy link
Contributor Author

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants