diff --git a/internal/query/statement/insert.go b/internal/query/statement/insert.go index be44b66c..8875ea58 100644 --- a/internal/query/statement/insert.go +++ b/internal/query/statement/insert.go @@ -38,13 +38,13 @@ func (stmt *InsertStmt) Prepare(c *Context) (Statement, error) { var s *stream.Stream if stmt.Values != nil { + ti, err := c.Catalog.GetTableInfo(stmt.TableName) + if err != nil { + return nil, err + } + // if no fields have been specified, we need to inject the fields from the defined table info if len(stmt.Fields) == 0 { - ti, err := c.Catalog.GetTableInfo(stmt.TableName) - if err != nil { - return nil, err - } - for i := range stmt.Values { kvs, ok := stmt.Values[i].(*expr.KVPairs) if !ok { @@ -61,6 +61,15 @@ func (stmt *InsertStmt) Prepare(c *Context) (Statement, error) { } } } + } else { + if !ti.FieldConstraints.AllowExtraFields { + for i := range stmt.Fields { + _, ok := ti.FieldConstraints.ByField[stmt.Fields[i]] + if !ok { + return nil, errors.Errorf("table has no field %s", stmt.Fields[i]) + } + } + } } s = stream.New(docs.Emit(stmt.Values...)) } else { diff --git a/sqltests/INSERT/values.sql b/sqltests/INSERT/values.sql index 17e57383..e8ef1c64 100644 --- a/sqltests/INSERT/values.sql +++ b/sqltests/INSERT/values.sql @@ -23,6 +23,11 @@ SELECT pk(), * FROM test; } */ +-- test: VALUES, with too many fields +CREATE TABLE test (a TEXT, b TEXT, c TEXT); +INSERT INTO test (b, a, c, d) VALUES ('b', 'a', 'c', 'd'); +-- error: table has no field d + -- test: variadic, VALUES, with all fields CREATE TABLE test (a TEXT, b TEXT, c TEXT, ...); INSERT INTO test (a, b, c) VALUES ('a', 'b', 'c');