New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Insert code of golang template seems to be incorrect #347
Comments
This is not an issue with the template. It seems as if the column is not being detected (more likely it wasn't being set) as autoincrement. All other examples handle this find. This does not impact anything but the example. |
yes this is not issue with template rather it is issue with PK not getting detected. It impacts all sqlite databases as PK will not be detected. In my code i am using autoincrement and it is still inserting the id's. SQLite Auto PK Logic
|
The autoincrement logic works fine for |
I did bit of digging and it looks like the sql generated by xo is not matching with that of schema however json of schema generated by xo is identifying the fields correctly. here is the schema, generated schema and json |
The cause of the errors is the comments within the table declaration, and their interference with the string manipulation to determine which column is autoincrement. This is necessary because sqlite does not provide a mechanism to properly get the sequences of a table (at least I was unable to find any better solution at the time of writing the query). If possible, try to strip out the comments. I'll look into if I can fix my SQL query, but it's crafty, and frankly it's not really high priority at the moment. |
Ok, i removed the comments and result is still the same. |
You can use |
fyi sqlite pragma table_info() indicates if the field is pk. May be this can be used in addition to the current heuristic that is getting used to identify the primary table sample output is shown below |
https://github.com/xo/xo/blob/master/_examples/django/sqlite3/author.xo.go
// insert (primary key generated and returned by database)
const sqlstr =
INSERT INTO authors (
+author_id, name
+) VALUES (
+$1, $2
+)
// run
logf(sqlstr, a.Name)
res, err := db.ExecContext(ctx, sqlstr, a.AuthorID, a.Name)
In this code author_id should not be sent to database unless id's are getting maintained by the application which does not seems to be the case as later on it retrieves and sets the id
// retrieve id
id, err := res.LastInsertId()
if err != nil {
return logerror(err)
} // set primary key
a.AuthorID = int(id)
Earlier version of xo was producing correct code, i am not sure when was this issue introduced and how is this not caught in testing.
The text was updated successfully, but these errors were encountered: