dbal icon indicating copy to clipboard operation
dbal copied to clipboard

Fix "bigint" type introspection for PK column with AI for SQLite

Open mvorisek opened this issue 1 year ago • 14 comments
trafficstars

Q A
Type bug
Fixed issues #6396

Summary

Because SQLite does not support AUTOINCREMENT keyword on any other type than INTEGER [1] [2], bigint type is mapped to INTEGER SQLite database column by DBAL already [3].

As we remap the type on table creation, we need to remap the type also on table introspection. The reason, we remap integer to bigint, is bigint is subtype of integer - PK defined/created originally as bigint must be always introspected as bigint.

[1] https://www.sqlite.org/autoinc.html [2] https://dbfiddle.uk/yyXvHV-6 [3] https://github.com/doctrine/dbal/blob/3.8.4/src/Platforms/SqlitePlatform.php#L271-L274

mvorisek avatar May 28 '24 18:05 mvorisek

It would seem that this is going to cause the opposite problem: a primary key with type INTEGER is wrongly going to be introspected as BIGINT, isn't it?

This is unavoidable because of #5107 and expected - INT is subtype of BIGINT, so BIGINT must be introspected because of https://github.com/doctrine/dbal/blob/3.8.4/src/Platforms/SqlitePlatform.php#L271-L274 transformation done when creating the table.

mvorisek avatar Jun 02 '24 09:06 mvorisek

The auto increment are in the sequence table and these are integers probably. You could select from this table to check if there is auto increment.

remco-pc avatar Jun 04 '24 12:06 remco-pc

Is it really wanted to do additional query in _getPortableTableColumnDefinition method?

mvorisek avatar Jun 05 '24 16:06 mvorisek

I'm not comfortable with merging this change into 3.8, tbh.

As you've already noticed, SQLite's implementation of integer PKs is a bit odd. The main issue is that SQLite does not allow you to create BIGINT PKs which is why the DBAL creates an INTEGER PRIMARY KEY column instead which has the value range of a BIGINT.

This also means that when introspecting, we cannot tell the difference between a column that was created by DBAL from a IntegerType and one that was created from a BigIntType. This is why the DBAL guesses one of two possible types. Apparently the wrong one in your case. Your "fix" would simply change that guess to a different one. That guess is not more accurate that the currently implemented one. It's just wrong for a different group of apps.

derrabus avatar Jul 16 '24 08:07 derrabus

You could also solve this in documentation: say in sqlite primary keys are just ints, not bigints and do nothing about the code or throw an exception when a primary key is not an int in sqlite

remco-pc avatar Jul 16 '24 11:07 remco-pc

throw an exception when a primary key is not an int in sqlite

We probably don't want this. If someone uses BIGINT PKs on their entity model, e.g. because they're using MySQL in production, they should still be able to deploy their model to SQLite for testing purposes.

derrabus avatar Jul 16 '24 11:07 derrabus

Yes and testing a big int number will fail

remco-pc avatar Jul 16 '24 12:07 remco-pc

I think the point was that it shouldn't fail. Prod uses MySQL and a Test environment SQLite. It would break projects if SQLite would suddenly throw an exception. If it would stay that way, I wouldn't expect such a change in 3.8 or 4.0.

SenseException avatar Jul 27 '24 20:07 SenseException

There hasn't been any activity on this pull request in the past 90 days, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days. If you want to continue working on it, please leave a comment.

github-actions[bot] avatar Nov 13 '24 03:11 github-actions[bot]

I will work on this.

mvorisek avatar Nov 13 '24 08:11 mvorisek

There hasn't been any activity on this pull request in the past 90 days, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days. If you want to continue working on it, please leave a comment.

github-actions[bot] avatar Feb 12 '25 03:02 github-actions[bot]

still will work on this

mvorisek avatar Feb 12 '25 10:02 mvorisek

There hasn't been any activity on this pull request in the past 90 days, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days. If you want to continue working on it, please leave a comment.

github-actions[bot] avatar May 14 '25 03:05 github-actions[bot]

still will work on this

mvorisek avatar May 14 '25 08:05 mvorisek

There hasn't been any activity on this pull request in the past 90 days, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days. If you want to continue working on it, please leave a comment.

github-actions[bot] avatar Oct 09 '25 03:10 github-actions[bot]

still will work on this

mvorisek avatar Oct 09 '25 05:10 mvorisek