You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Actually I'm not sure that this is bug but... I found a little confusing behaviour. It happens when I try to use in Insert().FromQuery() a subquery created with simple goqu.From, not with a concrete database dialect.
To Reproduce
If applicable provide a small code snippet to reproduce the issue (it's based on suite from mysql_test.go):
func (mt *mysqlTest) TestInsertFromSelect() {
ds := mt.db.From("entry")
// subquery created with goqu, if use here mt.db.Select... it works fine
subquery := goqu.Select(
goqu.V(11),
goqu.V(11),
goqu.C("float"),
goqu.C("string"),
goqu.C("time"),
goqu.C("bool"),
goqu.C("bytes"),
).From(goqu.T("entry")).Where(goqu.C("int").Eq(9))
query := ds.Insert().Cols().FromQuery(subquery)
s, _, err := query.ToSQL()
fmt.Println(s)
mt.NoError(err)
_, err = query.Executor().Exec()
mt.NoError(err)
}
Output:
=== RUN TestMysqlSuite
=== RUN TestMysqlSuite/TestInsertFromSelect
INSERT INTO `entry` SELECT 11, 11, "float", "string", "time", "bool", "bytes" FROM "entry" WHERE ("int" = 9)
--- FAIL: TestMysqlSuite (0.03s)
--- FAIL: TestMysqlSuite/TestInsertFromSelect (0.03s)
mysql_test.go:400:
Error Trace: mysql_test.go:400
Error: Received unexpected error:
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"entry" WHERE ("int" = 9)' at line 1
Test: TestMysqlSuite/TestInsertFromSelect
Here we see that subquery used a default dialect's escaping for columns and table names.
Expected behavior
I expected to have the same dialect as I used in main insert query.
Dialect:
postgres (I did not check it on postgres, but I think it will be the same)
mysql
sqlite3 (SQLite has the same escaping rules as default dialect, so it does not happen)
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered:
torwald-sergesson
changed the title
Using default dialect for goqu.T() in FromQuery().
Using default dialect for subquery in FromQuery()
May 23, 2020
I guess this happens because subquery during call FromQuery or generator during ToSQL do not override subquery dialect. And I'm not sure shall it be fixed, fail with error that queries used different dialects or just mentioned in docs as a requirement.
IMO it could be convenient to write subqueries without explicit dialect specification, at least it will be less verbose.
The fix could be trivial, just override nested SELECT dialect, something like
// Adds a subquery to the insert. See examples.
func (id *InsertDataset) FromQuery(from exp.AppendableExpression) *InsertDataset {
if sds, ok := from.(*SelectDataset); ok {
sds.dialect = id.dialect
}
return id.copy(id.clauses.SetFrom(from))
}
Describe the bug
Actually I'm not sure that this is bug but... I found a little confusing behaviour. It happens when I try to use in Insert().FromQuery() a subquery created with simple goqu.From, not with a concrete database dialect.
To Reproduce
If applicable provide a small code snippet to reproduce the issue (it's based on suite from mysql_test.go):
Output:
Here we see that subquery used a default dialect's escaping for columns and table names.
Expected behavior
I expected to have the same dialect as I used in main insert query.
Dialect:
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: