-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Convert int to string when execute insert query if the type of mysql column is 'bigint unsigned' #14663
Comments
Have you checked that it is not related to PDO ? |
This is not a bug. The maximum int value of php depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. see http://php.net/manual/en/language.types.integer.php for more details |
I think it's the programmer's responsibility to take care of the risk of int overflow. |
how did you verify the type of the value bound on query execution time? These are bound as parameter values and are converted inside of PDO, Yii has no logic to convert int to string by default in that case. I can not reproduce the issue. Please provide the CREATE TABLE statement of your table. |
I find some slow query in my production MySQL slow log which the type of value is not as expected. Maybe the performance of type casting is not the key reason for mysql, but I still think it's better not to cast the type of value if user set it explicitly.
I think the following code do the casting. yii2/framework/db/mysql/QueryBuilder.php Line 261 in 1501c65
Insert any table which has |
yeah, the reason for this is that bigint unsigned is bigger than integer in PHP so the string representation is safe to use, while integer may overflow. |
can you provide more information about this? What exactly is logged in MySQL? |
The table has a union index with two unsigned bigint columns and has ten million of rows. There are some inserted sql logged at MySQL slow query log. Although I think it's not a heavy work doing type casting for mysql, I still migrated the unsigned bigint to signed.
The overflow only happened when string casted to int, but if programmer set the type as int explicitly, my point is that maybe it's not necessary to cast int back to string. |
it is probably not necessary, but we need to find the condition to find that case. |
I'm check query for this code $model = new Order();
$model->customer_id = 1000000000000;
$model->save(false); Result query is
|
for the $model->customer_id = 10000000000000000000000; sql INSERT INTO `order` (`customer_id`, `created_at`) VALUES (1864712049423024128, 1612863706)
|
|
You may add rules to model. But generated sql query is correct. |
@darkdef What is the type of |
You right. Reproduced the problem |
The value |
* write adequate test for issue #14663 * don't convert int to string if db type of column is numeric (#14663) * fix bigint schema test for MySql >8.0.17 (#14663) * Update CHANGELOG.md * Update CHANGELOG.md * update phpdoc [ci skip] (#14663) * refactoring test case to make it clearer [ci skip] (#14663) * check `int unsigned` in `QueryBuilderTest::testInsertInteger()` (#14663) * Update Upgrade.md (#14663) * fix `int unsigned` schema test for MySql >8.0.17 (#14663) * fix `int unsigned` schema test for MySql <5.7 (#14663) Co-authored-by: Alexander Makarov <sam@rmcreative.ru> Co-authored-by: Bizley <pawel@positive.codes>
Closed via 17742cb |
…18741) * write adequate test for issue yiisoft#14663 * don't convert int to string if db type of column is numeric (yiisoft#14663) * fix bigint schema test for MySql >8.0.17 (yiisoft#14663) * Update CHANGELOG.md * Update CHANGELOG.md * update phpdoc [ci skip] (yiisoft#14663) * refactoring test case to make it clearer [ci skip] (yiisoft#14663) * check `int unsigned` in `QueryBuilderTest::testInsertInteger()` (yiisoft#14663) * Update Upgrade.md (yiisoft#14663) * fix `int unsigned` schema test for MySql >8.0.17 (yiisoft#14663) * fix `int unsigned` schema test for MySql <5.7 (yiisoft#14663) Co-authored-by: Alexander Makarov <sam@rmcreative.ru> Co-authored-by: Bizley <pawel@positive.codes>
What steps will reproduce the problem?
Save a model
What is the expected result?
The query should be
What do you get instead?
The executed query is
The range of MySQL unsigned bigint is larger than PHP_INT_SIZE, but it's not reasonable to convert int attribute manually set to string when execute sql, which may slow the query performance.
Additional info
The text was updated successfully, but these errors were encountered: