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

Cannot create migration to drop default value of a Postgres column #12135

Closed
starrychloe opened this issue Aug 10, 2016 · 3 comments
Closed

Cannot create migration to drop default value of a Postgres column #12135

starrychloe opened this issue Aug 10, 2016 · 3 comments
Milestone

Comments

@starrychloe
Copy link

What steps will reproduce the problem?

// remove default values to force setting network_id
$this->alterColumn('account', 'network_id', $this->integer()->notNull());
$this->alterColumn('account', 'network_id', $this->integer()->dropDefault()); # this function doesn't 
exist

What is the expected result?

Drop the default value

What do you get instead?

    > alter column network_id in table account to integer NOT NULL ...Exception 'yii\db\Exception' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "NOT"
LINE 1: ...LE "account" ALTER COLUMN "network_id" TYPE integer NOT NULL
                                                               ^
The SQL being executed was: ALTER TABLE "account" ALTER COLUMN "network_id" TYPE integer NOT NULL'

Additional info

http://www.yiiframework.com/doc-2.0/yii-db-migration.html#alterColumn()-detail
https://www.postgresql.org/docs/current/static/sql-altertable.html
ALTER [ COLUMN ] column_name DROP DEFAULT

Q A
Yii version 2.0.8
PHP version 5.6
Operating system Win 8.1
Postgres 9.5
@samdark
Copy link
Member

samdark commented Aug 10, 2016

Looks like DROP DEFAULT is supported by MySQL and PostgreSQL.

SQLite doesn't support altering table schema.

When it comes to MSSQL, it's a bit more complicated: http://stackoverflow.com/questions/10777353/drop-default-constraint-on-a-column-in-tsql

Not sure about Oracle.

@starrychloe
Copy link
Author

Also trying to remove not null by altering the column causes a problem.

Yii::$app->db->createCommand('alter table sitelink alter column campaign_id drop not null')->execute();
// $this->alterColumn('sitelink', 'campaign_id', $this->bigInteger()); // remove not null

Just using alterColumn() does nothing.

ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL

@samdark samdark added this to the 2.0.x milestone Aug 13, 2016
@Carw
Copy link

Carw commented Aug 17, 2016

Same problem.

$this->alterColumn('provider', 'alias', $this->string(255)->notNull()->unique());

Error:

LINE 1: ..."provider" ALTER COLUMN "alias" TYPE varchar(255) NOT NULL U...
                                                             ^
The SQL being executed was: ALTER TABLE "provider" ALTER COLUMN "alias" TYPE varchar(255) NOT NULL UNIQUE'

Correct SQL code:

ALTER TABLE "provider" ALTER COLUMN "alias" TYPE varchar(255),
ALTER COLUMN "alias" SET NOT NULL, ADD UNIQUE ("alias");

@samdark samdark modified the milestone: 2.0.x Dec 18, 2017
@samdark samdark added status:under development Someone is working on a pull request. and removed type:enhancement labels Apr 24, 2019
samdark pushed a commit that referenced this issue Apr 30, 2019
…n()` to accept properly `ColumnSchemaBuilder` definition of column
@samdark samdark added this to the 2.0.19 milestone Apr 30, 2019
@samdark samdark removed the status:under development Someone is working on a pull request. label Apr 30, 2019
@samdark samdark closed this as completed Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants