yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Implicit conversion from data type char to varbinary(max) is not allowed #16479

Closed sdlins closed 5 years ago

sdlins commented 6 years ago

I have this RBAC migration:

    public function safeUp()
    {
        /** @var \yii\rbac\DbManager $rbac */
        $rbac = \Yii::$app->get('authManagerAlteracaoDeCronograma');

        $solicitante = $rbac->createRole('solicitante');
        ...

        $rbac->add($solicitante); //error happens inside this
        ...
    }

That is resulting in this error:

*** applying m180704_153354_rbac_permissoes_e_regramento
Exception: SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Implicit conversion from data type char to varbinary(max) is not allowed. Use the CONVERT function to run this query.
The SQL being executed was: INSERT INTO [ALTERACAOCRONO_RBAC_ITEM] ([name], [type], [description], [rule_name], [data], [created_at], [updated_at]) VALUES ('solicitante', 1, NULL, NULL, NULL, 1530722672, 1530722672)

The most strange is that when I execute the sql pointed in the error right in the MSSQL manager it works fine.

I tryed to change the field data from the type binary to string and rerun the migration just to check the behavior and it works fine too. So I think that QueryBuilder::normalizeTableRowData() could be failing somehow that I still could not discover debugging the code.

Additional info

Q A
Yii version 2.0.15.1
PHP version 7.1.14
Operating system Win 10
MSSQL 2016
samdark commented 6 years ago

Try master.

sdlins commented 6 years ago

Did you mean master db? I didn't get you @samdark.

samdark commented 6 years ago

master branch of Yii instead 2.0.15.1

sdlins commented 6 years ago

Oh, sorry. I will try.

sdlins commented 6 years ago

Tried against a dev-master install and now the error message and the sql is different:

Exception: SQLSTATE[07002]: [Microsoft][ODBC Driver 13 for SQL Server]COUNT field incorrect or syntax error
The SQL being executed was: INSERT INTO [ALTERACAOCRONO_RBAC_ITEM] ([name], [type], [description], [rule_name], [data], [created_at], [updated_at]) VALUES (NULL, 1, NULL, NULL, CONVERT(VARBINARY, NULL), 1530815512, 1530815512) (C:\...\vendor\yiisoft\yii2\db\Schema.php:664)

When I get home I will try everything in my LAMP environment to check the results.

samdark commented 6 years ago

Any idea what that means?

sdlins commented 6 years ago

Not really because I had not time to debug this time. But I will be able to debug within some minutes. I will update this answer after that.

-- @samdark, update: When we reach \yii\db\QueryBuilder::prepareInsertValues() we have empty $params and these $columns:

name:"solicitante"
type:1
description:null
rule_name:null
data:yii\db\Expression
    expression:"CONVERT(VARBINARY, :qp0)" // Please, note ":qp0"
params:array(1)
created_at:1530821884
updated_at:1530821884

And inside prepareInsertValues() we have this code:

if ($value instanceof ExpressionInterface) {
     $placeholders[] = $this->buildExpression($value, $params);

$params here are:

:qp0:"solicitante"
:qp1:1
:qp2:null
:qp3:null

But when \yii\db\ExpressionBuilder::build() is called inside buildExpression() all $params are merged with $expression->params. At this point my :qp0 is changed from 'solicitante' to null.

PS: Here I am talking about the last error as this comment above.

I tried a solution but since I am at work I cant dispend much time and at home I dont have win nor mssql. So, I hope to be able to help in next days but I cant guarantee that I will have enough time at work, sorry.

sdlins commented 5 years ago

Closing since normalizeTableRowData() seems fixed by @marcelodeandrade.