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
SQLite on_conflict not working with ORM #4567
Comments
I had thought maybe my sqlite version didn't have this feature, but it seems like it does according to the link below.
quoted from here: |
The main reason this is a problem is because I'm inserting a lot of data and checking to see if the data already exists is causing a 10x slowdown for a process that will take on the order of days already. |
Hiya . OK. So this is a hard one to go through, since the example you gave has found so many different, intuitive, and perfectly reasonable ways of achieving the behavior you are looking for, and each one of them is subtly incorrect. It's as though I asked you to run through a carwash blindfolded and you didn't get wet, if that makes sense. So there is definitely a bug here in that first of all the documentation is not telling it like it is, but additionally, there can very easily be warnings in the DDL renderer that also indicate that it looks like you're trying to do something and we can't do it the way you are asking. So the first thing to note is, for DDL things that are requested, SQLAlchemy is not going to look at the SQlite version in use. If you asked it to render an "ON CONFLICT" (and it's the way SQLAlchemy expected it to be asked), it's not going to silently pass on that for an older version of SQLite, because this means your program isn't going to work. This isn't an optimization, that is, it changes the behavior of the program. Next thing. You can't affect the primary key constraint of a Table object like this:
if you want to add the constraint after the fact, it has to be this:
However, you'll note in the examples, the primary key constraint is given inline. This is the most straighforward way to do it here, which when using declarative is done with the So at this point we can illustrate how your program does do what you want:
So next thing, all of these dialect specific arguments are always prefixed with the dialect name, so in the
that's in the docs. But what the docs need a BIG RED WARNING to say here is that, the column level sqlite_on_conflict_primary_key argument only applies to a single-column primary key. if you have a composite primary key, you have to use an explicit PrimaryKeyConstraint. This can be in the docs and the dialect can definitely emit a warning when it sees the column -level argument in a composite primary key. So the TODO list here is:
and then we're done! |
for consistency, all the paragraph level docs that are inline with |
Thanks so much for the detailed response! It did feel like going through a car-wash blindfolded and not getting wet haha. I had been looking for something like |
I've tried several different versions of this code to try to get primary key conflicts to be ignored on the table slippy_tiles but nothing seems to take when creating the schema from ORM, even though it is supposedly fixed in #4360
I've tried renaming the kwarg in the column definitions to sqlite_on_conflict_primary_key, sqlite_on_conflict, on_conflict, editing the primary key, making a new primary key constraint, nothing seems to get it to actually put the clause in the SQL or create a schema accordingly.
Prints this schema:
Which contains no on conflict clauses
The text was updated successfully, but these errors were encountered: