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

[BUG]: ALTER COLUMN FOR BOOLEAN #15829

Open
MaksimChernyavsky opened this issue Dec 13, 2021 · 4 comments
Open

[BUG]: ALTER COLUMN FOR BOOLEAN #15829

MaksimChernyavsky opened this issue Dec 13, 2021 · 4 comments
Labels
bug A bug report need script to reproduce Script is required to reproduce the issue status: unverified Unverified

Comments

@MaksimChernyavsky
Copy link

MaksimChernyavsky commented Dec 13, 2021

While updating default value for my boolean database column, i have an error while run migrations like

Runtime Error: Failed to modify column 'auto_tz' in table 'user_settings'. In 'UserSettingsMigration_100' migration. DB error: SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "COLUMN"
LINE 1:  ALTER COLUMN "auto_tz" SET DEFAULT false;

I suppose it is because code In class Phalcon\Db\Dialect\Postgresql (see image)
Screenshot 2021-12-13 at 19 33 00

@MaksimChernyavsky MaksimChernyavsky added bug A bug report status: unverified Unverified labels Dec 13, 2021
@Jeckerson Jeckerson changed the title ALTER COLUMN FOR BOOLEAN[BUG]: [BUG]: ALTER COLUMN FOR BOOLEAN Dec 13, 2021
@Jeckerson
Copy link
Member

Hello, what is the version of migrations?

@MaksimChernyavsky
Copy link
Author

MaksimChernyavsky commented Dec 14, 2021

I got it from "phalcon/devtools": "^4.1", in my composer.json, but I suppose that problem is in Phalcon project. It does not add ALTER TABLE part to sql request when you try to change your boolean column default value.
I have a table

\d test_table;
                   Table "psn.test_table"
  Column   |   Type   | Collation | Nullable |    Default    
-----------+----------+-----------+----------+---------------
 int_prop  | smallint |           |          | '1'::smallint
 bool_prop | boolean  |           |          | true

And this code

use Phalcon\Db\Column as Column;
$db = new Phalcon\Db\Adapter\Pdo\Postgresql($config);

$aColumn = [ 'type' => Column::TYPE_SMALLINTEGER, 'default' => "2" ];
$oColumn = new Column('int_prop', $aColumn);
$aColumn = [ 'type' => Column::TYPE_SMALLINTEGER, 'default' => "1" ];
$oCurrentColumn = new Column('int_prop', $aColumn);
if ($sRes = $db->modifyColumn('test_table', 'psn', $oColumn, $oCurrentColumn) !== true)
  print($sRes);

$aColumn = [ 'type' => Column::TYPE_BOOLEAN, 'default' => false ];
$oColumn = new Column('bool_prop', $aColumn);
$aColumn = [ 'type' => Column::TYPE_BOOLEAN, 'default' => true ];
$oCurrentColumn = new Column('bool_prop', $aColumn);
if ($sRes = $db->modifyColumn('test_table', 'psn', $oColumn, $oCurrentColumn) !== true)
  print($sRes);

successfully update default for smallint, but fails with error:

PHP Fatal error:  Uncaught PDOException: SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "COLUMN"
LINE 1:  ALTER COLUMN "bool_prop" SET DEFAULT ;

while updating boolean default value
my phalcon version 4.1.0

@MaksimChernyavsky
Copy link
Author

                if memstr(strtoupper(columnDefinition), "BOOLEAN") {
                    let sql .= " ALTER COLUMN \"" . column->getName() . "\" SET DEFAULT " . defaultValue;
                } else {
                    let sql .= sqlAlterTable . " ALTER COLUMN \"" . column->getName() . "\" SET DEFAULT " . defaultValue;
                }

this IF in Phalcon\Db\Dialect\Postgresql makes this error(

@niden
Copy link
Member

niden commented Jan 4, 2022

@MaksimChernyavsky do me a favor please. Can you post what the correct SQL should be for PostgreSql (if you were to write a raw query) in order to update the bool column?

I apologize but I have not worked with PostgreSql enough to know this

@niden niden added the need script to reproduce Script is required to reproduce the issue label Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report need script to reproduce Script is required to reproduce the issue status: unverified Unverified
Projects
Status: Backlog
Development

No branches or pull requests

3 participants