Improving the detection of auto-incrementing primary keys [done]
Posted: Fri Oct 27, 2023 5:00 pm
The native format of nuBuilder primary keys (PKs) is a VARCHAR, which is populated using PHP's uniqid() function.
The reasons for this are explained in https://nubuilder.blogspot.com/2010/09/nubuilder-primary-keys.html
However, some of us may have to deal with pre-existing tables which use auto-incrementing integers (usually bigint) as PKs.
Happily nuBuilder will detect tables of this type (in most cases) and the correct PKs will be generated. This is achieved by the function
The return value is currently determined by the expression:
However, if we are using an updateable view, for example, the Extra COLUMN is empty and so this does not work.
The work around, if the developer knows that all integer values used as primary keys are auto-increment, is to use the row type and look for 'int', i.e. if it is 'int' it is 'auto_increment'.
This gives a modified return expression of:
If we wish to preserve full conformity with the legacy code, we can introduce a new global $nuConfig variable which we will call
$nuConfigIntegerPKsAuto , which would have a default value of false. The expression then becomes
so that the RHS of the expression only evaluates to true if $nuConfigIntegerPKsAuto is set to true in the configuration settings.
The modification of the function db_is_auto_id() along these lines would be most welcome.
No doubt there are other ways but this has been tested and it works.
Thanks
Neil
The reasons for this are explained in https://nubuilder.blogspot.com/2010/09/nubuilder-primary-keys.html
However, some of us may have to deal with pre-existing tables which use auto-incrementing integers (usually bigint) as PKs.
Happily nuBuilder will detect tables of this type (in most cases) and the correct PKs will be generated. This is achieved by the function
Code: Select all
db_is_auto_id($table, $primaryKey)
Code: Select all
return $row->Extra == 'auto_increment';
The work around, if the developer knows that all integer values used as primary keys are auto-increment, is to use the row type and look for 'int', i.e. if it is 'int' it is 'auto_increment'.
This gives a modified return expression of:
Code: Select all
return ($row->Extra == 'auto_increment' || str_contains($row->Type, 'int'));
If we wish to preserve full conformity with the legacy code, we can introduce a new global $nuConfig variable which we will call
$nuConfigIntegerPKsAuto , which would have a default value of false. The expression then becomes
Code: Select all
return ($row->Extra == 'auto_increment' || ($nuConfigIntegerPKsAuto && str_contains($row->Type, 'int')));
The modification of the function db_is_auto_id() along these lines would be most welcome.
No doubt there are other ways but this has been tested and it works.
Thanks
Neil