From 9b6b5d1b696e135fc7042c2f76451f1ed60b5532 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 2 Apr 2021 21:54:31 +0200 Subject: [PATCH] Make rowid aliases non-nullable (#1128) --- sqlparser/CHANGELOG.md | 1 + sqlparser/lib/src/analysis/schema/table.dart | 3 +++ .../test/analysis/schema/from_create_table_test.dart | 10 ++++++++++ 3 files changed, 14 insertions(+) diff --git a/sqlparser/CHANGELOG.md b/sqlparser/CHANGELOG.md index cf848264..7c875d20 100644 --- a/sqlparser/CHANGELOG.md +++ b/sqlparser/CHANGELOG.md @@ -2,6 +2,7 @@ - New analysis checks for `RETURNING`: Disallow `table.*` syntax and aggregate expressions - Fix resolving columns when `RETURNING` is used in an `UPDATE FROM` statement +- Fix aliases to rowid being reported as nullable ## 0.15.0 diff --git a/sqlparser/lib/src/analysis/schema/table.dart b/sqlparser/lib/src/analysis/schema/table.dart index 797c75e5..87c94d84 100644 --- a/sqlparser/lib/src/analysis/schema/table.dart +++ b/sqlparser/lib/src/analysis/schema/table.dart @@ -53,6 +53,9 @@ class Table extends NamedResultSet with HasMetaMixin implements HumanReadable { if (_rowIdColumn == null && column.isAliasForRowId()) { _rowIdColumn = column; + // By design, the rowid is non-nullable, even if there isn't a NOT NULL + // constraint set on the column definition. + column._type = const ResolvedType(type: BasicType.int, nullable: false); } } } diff --git a/sqlparser/test/analysis/schema/from_create_table_test.dart b/sqlparser/test/analysis/schema/from_create_table_test.dart index 7e3fb4e7..d390213b 100644 --- a/sqlparser/test/analysis/schema/from_create_table_test.dart +++ b/sqlparser/test/analysis/schema/from_create_table_test.dart @@ -127,4 +127,14 @@ void main() { final table = engine.schemaReader.read(stmt as CreateTableStatement); expect(table.resolvedColumns.single.type.type, BasicType.blob); }); + + test('aliases to rowid are non-nullable', () { + final engine = SqlEngine(); + final stmt = + engine.parse('CREATE TABLE foo (id INTEGER PRIMARY KEY);').rootNode; + + final table = engine.schemaReader.read(stmt as CreateTableStatement); + expect(table.resolvedColumns.single.type, + const ResolvedType(type: BasicType.int, nullable: false)); + }); }