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
truncate() on SQLite does not reset the id value in the sqlite_sequence any longer #2312
Comments
Thanks for report, looks like there are various places where identifiers are not wrapped properly. |
This works on previous versions? |
@richraid21 No. Those plain string replacements has been always there. |
Well... |
Did you check the query (sql string) was throwing the I mean it really seems to set quotes correctly https://runkit.com/embed/g4idd055djit (I suppose this.tableName already had quotes around it). Maybe the code throws an error because some column / table which actually didn't exist earlier, now throws an error (check last comment in #1048)? And if you add Does this make sense? |
Hmm I'm not sure I understand you correctly. Back-ticks aren't the right quotes for this statement. The table names are strings in the `sqlite_sequence` table's `name` column, so running this doesn't work:
This works as expected:
Using back-ticks makes SQLite think that `table_name` is a column name to match against within the same `sqlite_sequence` table, while we really want to match against the actual name of the table, as it's stored in the `sqlite_sequence` table's `name` column, e.g.:
And regardless of this, there is no guarantee really that this query is executed before we use the table again in another query, because nothing is waiting for it t complete, therefore the reset might currently occur too late. Even though that's rather unlikely, this issue should also be addressed with a fix that waits for the 2nd sql statement to complete too. |
I think the right thing to do is:
|
@lehni You are right, I didn't thought that table name as column value. Shouldn't |
I'm not familiar enough with it to say which quotes are best. Maybe it's better to pass the value as an actual binding to the sql statement? |
Yep, that would be the best option 👌 |
I can give this a go but I have no idea what to do about the two queries and the promise of the 2nd. Could the queries simply be joined by |
I also can't tell if |
Also, shouldn't these two queries really run in one transaction, so that if something goes wrong, both are undone and an error is produced instead of just swallowed? |
@lehni they shouldn't be forced to transaction unless it is initialized by the user. Both queries should be able to be done by |
Hmm but I don't think I can call
with `truncate` as (delete from `${table_name}`) delete from sqlite_sequence where name = '${table_name}' |
Also, I don't think the statement in the parentheses of a with statement can be a delete command. I think it needs to be a select statement:
|
And lastly, isn't |
I was trying out some sqlite CTEs and indeed it seems that with sqlite one cannot do any updates in those... I need to look more to it. About the returned value, I suppose count if fine, probably most of the dialects just return something truthy. |
I'd like to suggest two measures here:
|
That would be good start. I got no problem with that. |
Ok, I will tackle this now... First a simple commit to address 1), and I have some ideas for 2) as well: What if |
It also looks like the SQLite binding doesn't currently support schema, which they could / should? That would be a separate issue: https://www.sqlite.org/lang_attach.html |
Relates to knex#2312: ids were not correctly reset anymore
Here the proposed fix for 1) now: 9b724fb |
Relates to knex#2312: ids were not correctly reset anymore
Relates to knex#2312: ids were not correctly reset anymore
Relates to knex#2312: ids were not correctly reset anymore
Relates to knex#2312: ids were not correctly reset anymore
Relates to knex#2312: ids were not correctly reset anymore
Relates to #2312: ids were not correctly reset anymore
🎉 |
Upon upgrading to 0.14.0, I've realised that my primary keys don't get reset anymore when I use
truncate()
with the SQLite dialect.It looks like the code that's supposed to do the reseting in
truncate()
is actually producing an error:https://github.com/tgriesser/knex/blob/bf8aa4db100ed34756d1d77268e3b391d5d2b528/src/dialects/sqlite3/query/compiler.js#L89-L99
If you replace
noop
there with(err) => { console.log(err) }
, you will see the errors logged:It looks like the table-name should be quoted in that SQL statement. If I manually add quotes, then the ids get reset again:
sql: `delete from sqlite_sequence where name = "${table}"`
I'm not able to say if that's the correct fix for this issue.
The text was updated successfully, but these errors were encountered: