@@ -2973,7 +2973,25 @@ private function translate_insert_or_replace_body_in_non_strict_mode(
2973
2973
}
2974
2974
}
2975
2975
2976
- // 3. Get the list of column names returned by VALUES or SELECT clause.
2976
+ // 3. Filter out omitted columns that will get a value from the SQLite engine.
2977
+ // That is, nullable columns, columns with defaults, and generated columns.
2978
+ $ columns = array_values (
2979
+ array_filter (
2980
+ $ columns ,
2981
+ function ( $ column ) use ( $ insert_list ) {
2982
+ $ is_omitted = ! in_array ( $ column ['COLUMN_NAME ' ], $ insert_list , true );
2983
+ if ( ! $ is_omitted ) {
2984
+ return true ;
2985
+ }
2986
+ $ is_nullable = 'YES ' === $ column ['IS_NULLABLE ' ];
2987
+ $ has_default = $ column ['COLUMN_DEFAULT ' ];
2988
+ $ is_generated = str_contains ( $ column ['EXTRA ' ], 'auto_increment ' );
2989
+ return ! ( $ is_nullable || $ has_default || $ is_generated );
2990
+ }
2991
+ )
2992
+ );
2993
+
2994
+ // 4. Get the list of column names returned by VALUES or SELECT clause.
2977
2995
$ select_list = array ();
2978
2996
if ( 'insertQueryExpression ' === $ node ->rule_name ) {
2979
2997
// When inserting from a SELECT query, we don't know the column names.
@@ -2994,28 +3012,27 @@ private function translate_insert_or_replace_body_in_non_strict_mode(
2994
3012
}
2995
3013
}
2996
3014
2997
- // 4 . Compose a new INSERT field list with all columns from the table.
3015
+ // 5 . Compose a new INSERT field list with all columns from the table.
2998
3016
$ fragment = '( ' ;
2999
3017
foreach ( $ columns as $ i => $ column ) {
3000
3018
$ fragment .= $ i > 0 ? ', ' : '' ;
3001
3019
$ fragment .= $ this ->quote_sqlite_identifier ( $ column ['COLUMN_NAME ' ] );
3002
3020
}
3003
3021
$ fragment .= ') ' ;
3004
3022
3005
- // 5 . Compose a wrapper SELECT statement emulating IMPLICIT DEFAULT values.
3023
+ // 6 . Compose a wrapper SELECT statement emulating IMPLICIT DEFAULT values.
3006
3024
$ fragment .= ' SELECT ' ;
3007
3025
foreach ( $ columns as $ i => $ column ) {
3008
3026
$ is_omitted = ! in_array ( $ column ['COLUMN_NAME ' ], $ insert_list , true );
3009
3027
$ fragment .= $ i > 0 ? ', ' : '' ;
3010
3028
if ( $ is_omitted ) {
3011
- // When a column value is omitted from the INSERT statement, we
3012
- // need to use the DEFAULT value or the IMPLICIT DEFAULT value.
3013
- $ is_auto_inc = str_contains ( $ column ['EXTRA ' ], 'auto_increment ' );
3014
- $ is_nullable = 'YES ' === $ column ['IS_NULLABLE ' ];
3015
- $ default = $ column ['COLUMN_DEFAULT ' ];
3016
- if ( null === $ default && ! $ is_nullable && ! $ is_auto_inc ) {
3017
- $ default = self ::DATA_TYPE_IMPLICIT_DEFAULT_MAP [ $ column ['DATA_TYPE ' ] ] ?? null ;
3018
- }
3029
+ /*
3030
+ * When a column is omitted from the INSERT list, we need to use
3031
+ * an IMPLICIT DEFAULT value. Note that at this point, all omitted
3032
+ * columns that will not get an implicit default are filtered out.
3033
+ * (That is, nullable, generated, and columns with true defaults.)
3034
+ */
3035
+ $ default = self ::DATA_TYPE_IMPLICIT_DEFAULT_MAP [ $ column ['DATA_TYPE ' ] ] ?? null ;
3019
3036
$ fragment .= null === $ default ? 'NULL ' : $ this ->connection ->quote ( $ default );
3020
3037
} else {
3021
3038
// When a column value is included, we need to apply type casting.
0 commit comments