Sqlserver: Numeric-Columns not working with more than 14 digits
This is a (multiple allowed):
- [x] bug
- CakePHP Version: 3.3.3.
- Platform and Target: IIS, PHP 7, MS SQL Server 2014
What you did
Create a Table like
CREATE TABLE [dbo].[example](
[id] [numeric](20, 0) NOT NULL PRIMARY KEY,
[col1] varchar(50),
[col2] varchar(50)
)
Bake the model cake bake model example
Example Query that works (length in WHERE is 14):
namespace App\Shell;
use Cake\Console\Shell;
class ExampleShell extends Shell
{
public function main()
{
$table = \Cake\ORM\TableRegistry::get('example');
$query = $table->find('all')->where(['id' => '12345678901234'])->first();
debug($query);
}
}
This debugs NULL.
Example Query that does not works (length in WHERE is 15):
namespace App\Shell;
use Cake\Console\Shell;
class ExampleShell extends Shell
{
public function main()
{
$table = \Cake\ORM\TableRegistry::get('example');
$query = $table->find('all')->where(['id' => '123456789012345'])->first();
debug($query);
}
}
The second example throws exeption shown below.
Expected Behavior
Both examples should work. In the second example the number is converted to 1.2345678901234E+14 (see SQL Server Profiler below). It should stay as maybe string, that would work.
Actual Behavior
If I want to SELECT rows with package_number length longer than 14 I get an error:
Exception: SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Error converting data type nvarchar to numeric. in ...\vendor\cakephp\cakephp\src\Database\Statement\StatementDecorator.php, line 172]
2016-09-07 10:49:29 Error: [PDOException] SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Error converting data type nvarchar to numeric.
With SQL Server Profiler I can see the queries executed on SQL server. On the first example (length 14):
declare @p1 int
set @p1=1073741828
declare @p2 int
set @p2=180150009
declare @p5 int
set @p5=8
declare @p6 int
set @p6=1
declare @p7 int
set @p7=0
exec sp_cursorprepexec @p1 output,@p2 output,N'@P1 nvarchar(4000)',N'SELECT TOP 1 example.id AS [example__id], example.col1 AS [example__col1], example.col2 AS [example__col2] FROM example example WHERE id = @P1',@p5 output,@p6 output,@p7 output,N'12345678901234'
select @p1, @p2, @p5, @p6, @p7
For the second example (length 15): With SQL-Server Profiler the query:
declare @p1 int
set @p1=NULL
declare @p2 int
set @p2=0
declare @p5 int
set @p5=4104
declare @p6 int
set @p6=8193
declare @p7 int
set @p7=0
exec sp_cursorprepexec @p1 output,@p2 output,N'@P1 nvarchar(4000)',N'SELECT TOP 1 example.id AS [example__id], example.col1 AS [example__col1], example.col2 AS [example__col2] FROM example example WHERE id = @P1',@p5 output,@p6 output,@p7 output,N'1.2345678901234E+14'
select @p1, @p2, @p5, @p6, @p7
can you show the result of debug($table->schema())
Sure:
########## DEBUG ##########
object(Cake\Database\Schema\Table) {
[protected] _table => 'example'
[protected] _columns => [
'id' => [
'type' => 'decimal',
'length' => '20',
'precision' => '0',
'null' => false,
'default' => null,
'baseType' => null,
'comment' => null,
'unsigned' => null
],
'col1' => [
'type' => 'string',
'length' => '50',
'null' => true,
'default' => null,
'collate' => 'Latin1_General_CI_AS',
'baseType' => null,
'precision' => null,
'comment' => null,
'fixed' => null
],
'col2' => [
'type' => 'string',
'length' => '50',
'null' => true,
'default' => null,
'collate' => 'Latin1_General_CI_AS',
'baseType' => null,
'precision' => null,
'comment' => null,
'fixed' => null
]
]
[protected] _typeMap => [
'id' => 'decimal',
'col1' => 'string',
'col2' => 'string'
]
[protected] _indexes => []
[protected] _constraints => [
'primary' => [
'type' => 'primary',
'columns' => [
(int) 0 => 'id'
],
'length' => []
]
]
[protected] _options => []
[protected] _temporary => false
[protected] _columnKeys => [
'type' => null,
'baseType' => null,
'length' => null,
'precision' => null,
'null' => null,
'default' => null,
'comment' => null
]
[protected] _columnExtras => [
'string' => [
'fixed' => null,
'collate' => null
],
'text' => [
'collate' => null
],
'integer' => [
'unsigned' => null,
'autoIncrement' => null
],
'biginteger' => [
'unsigned' => null,
'autoIncrement' => null
],
'decimal' => [
'unsigned' => null
],
'float' => [
'unsigned' => null
]
]
[protected] _indexKeys => [
'type' => null,
'columns' => [],
'length' => [],
'references' => [],
'update' => 'restrict',
'delete' => 'restrict'
]
[protected] _validIndexTypes => [
(int) 0 => 'index',
(int) 1 => 'fulltext'
]
[protected] _validConstraintTypes => [
(int) 0 => 'primary',
(int) 1 => 'unique',
(int) 2 => 'foreign'
]
[protected] _validForeignKeyActions => [
(int) 0 => 'cascade',
(int) 1 => 'setNull',
(int) 2 => 'setDefault',
(int) 3 => 'noAction',
(int) 4 => 'restrict'
]
}
###########################
This might be connected to how PHP handles floats for large number in general:
php > $n = '123456789012345'
php > ;
php > echo floatval($n);
1.2345678901234E+14
php >
php > echo intval($n);
123456789012345
Because your id column is a decimal, pdo/sqlsrv might be doing casting. CakePHP binds decimal/float values as PDO::PARAM_STR which should be retaining the string type when the parameters are bound to the query.
I'll see if this issue exists on SQLite, or MySQL as well.
Pull request up now.
the issue is not fixed for me with #9432. I am using CakePHP 3.3.6 now. Example:
$table = TableRegistry::get('Example');
$entity = $table->newEntity([
'id' => 13043396354000114154,
'col1' => 'col2',
'col2' => 'col2',
]);
debug($table->schema()); // #1
debug($entity); // #2
$table->save($entity);
debug($entity); //#3
$entity = $table->find('all')->first();
debug($entity); // #4
Result is:
// #1
object(Cake\Database\Schema\Table) {
[protected] _table => 'example'
[protected] _columns => [
'id' => [
'type' => 'decimal',
'length' => '20',
'precision' => '0',
'null' => false,
'default' => null,
'baseType' => null,
'comment' => null,
'unsigned' => null
],
'col1' => [
'type' => 'string',
'length' => '50',
'null' => true,
'default' => null,
'collate' => 'Latin1_General_CI_AS',
'baseType' => null,
'precision' => null,
'comment' => null,
'fixed' => null
],
'col2' => [
'type' => 'string',
'length' => '50',
'null' => true,
'default' => null,
'collate' => 'Latin1_General_CI_AS',
'baseType' => null,
'precision' => null,
'comment' => null,
'fixed' => null
]
]
[protected] _typeMap => [
'id' => 'decimal',
'col1' => 'string',
'col2' => 'string'
]
[protected] _indexes => []
[protected] _constraints => [
'primary' => [
'type' => 'primary',
'columns' => [
(int) 0 => 'id'
],
'length' => []
]
]
[protected] _options => []
[protected] _temporary => false
[protected] _columnKeys => [
'type' => null,
'baseType' => null,
'length' => null,
'precision' => null,
'null' => null,
'default' => null,
'comment' => null
]
[protected] _columnExtras => [
'string' => [
'fixed' => null,
'collate' => null
],
'text' => [
'collate' => null
],
'integer' => [
'unsigned' => null,
'autoIncrement' => null
],
'biginteger' => [
'unsigned' => null,
'autoIncrement' => null
],
'decimal' => [
'unsigned' => null
],
'float' => [
'unsigned' => null
]
]
[protected] _indexKeys => [
'type' => null,
'columns' => [],
'length' => [],
'references' => [],
'update' => 'restrict',
'delete' => 'restrict'
]
[protected] _validIndexTypes => [
(int) 0 => 'index',
(int) 1 => 'fulltext'
]
[protected] _validConstraintTypes => [
(int) 0 => 'primary',
(int) 1 => 'unique',
(int) 2 => 'foreign'
]
[protected] _validForeignKeyActions => [
(int) 0 => 'cascade',
(int) 1 => 'setNull',
(int) 2 => 'setDefault',
(int) 3 => 'noAction',
(int) 4 => 'restrict'
]
}
// #2
object(App\Model\Entity\Example) {
'id' => (float) 1.3043396354E+19,
'col1' => 'col2',
'col2' => 'col2',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'id' => true,
'col1' => true,
'col2' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Example'
}
// #3
object(App\Model\Entity\Example) {
'id' => (float) 1.3043396354E+19,
'col1' => 'col2',
'col2' => 'col2',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Example'
}
// #4
object(App\Model\Entity\Example) {
'id' => (float) 1.3043396354E+19,
'col1' => 'col2',
'col2' => 'col2',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Example'
}
Looking at the dataset in SQL Server Management Studio it contains the value 13043396354000113664 instead of 13043396354000114154.
Do you have any new ideas? Maybe I have to switch to a varchar(20) column and add some validation into the model...