-
Notifications
You must be signed in to change notification settings - Fork 3
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
Insert body breaks the order of fields #6
Comments
I do not maintain "https://androidrepo.com/repo/dzikoysk-exposed-upsert", that's probably just an automated site to scrap public repositories from GitHub. To make upsert work, you have to fulfill these requirements:
Does your methods have a custom logic/optional fields? I mean the logic behind:
|
both of these methods to just have except for the primary key field itself, I want to insert/upsert ALL fields of the table. So I came up with these helper methods to assure that only differene is the primary key itself, which is part of the insert, but obviously not of the update but the actual update statement that I see in the logs is not "off by 1" it is totally different. |
The problem with Exposed is that its internals are so... quite messy, not gonna like. It has a really strict policy about indexes, so if one of methods have extra entries, then it fails. I never tried to upsert table that extends another table, so there is a chance that it could be the root of the problem, as your insert & upsert body looks fine I think. |
well the inherited table columns are the same for every/any class, they just get added to each table as columns (don't know if there is something special here ... tried it, and it worked ... not intended to have any "meaning" more than I am lazy typing, so these columns get added to any table I deal with ... more seams like the index of the insertStatement is iterated over in kind of a HashSet type of manner ... so the indexes are totally different ...
the ones in the update are as they appear in the insertbody/updatebody |
I wouldn't be surprised if they'd sort this alphabetically and we can't do much about it if so :/ I think that the best way to go for you is just to move to 2 queries (select to find id and then create/update), that's what I did in my project to: |
Btw, could you try to sort your fields in insert & update alphabetically body? I'm just curious if it would work |
well, so that means: if you're using DAO approach, there seems to be some "magic" in IdTable that your upserts work but this "magic" doesn't happen in normal DSL Tables ... As for ordering the fields ... just tried it ... and it fails because: in the insert, you need to specify the primary key field, so the ordering "explodes" at the alphabetical position where your PK joins the concert. |
To be honest I always used it with DSL and never with DAO. Your case is something new, because it's not always sorting these fields automatically 🤔. Like I said, I probably won't be able to fix this as this is something about Exposed's internals that are full of dirty magic, so I'd suggest to just move on from this solution and safely cover it with these 2 queries if possible. Also, did you try to use the |
no blame on you at all ! Nothing you can do on it (at least not without considerable effort, which is definitely not worth it) but was worth a lucky shot for me to try it :) (will have a look at thanx for you super fast response and help! |
I've run into the field ordering issue, and think I might have solved it. My understanding is Assuming : Table.upsert( Exposed sorts the fields into alphabetical order. When the SQL is generated for the insertBody, these are correctly output as follows INSERT INTO tablename ("A","B","C","D") VALUES(1,2,3,4) When it comes to the updateBody, the field names are not sorted and are output in the order they are defined , but the values are output in the sorted order leading to corruption in the data : ON CONFLICT ON CONSTRAINT unique_constraint DO UPDATE SET B=1, C=2, A=3, D=4 I added the following to doUpdateSet : append(" DO UPDATE SET ") This now correctly generates : ON CONFLICT ON CONSTRAINT unique_constraint DO UPDATE SET A=1, B=2, C=3, D=4 I'm not sure if this fixes the whole problem, but in my case it appears to work. |
Do you want to try to submit PR? :) We have some integration tests on CI against all db targets, so we could verify if it works. |
a) repository link at bottom of page https://androidrepo.com/repo/dzikoysk-exposed-upsert is still broken
b)
if I have non DAO exposed Tables, is your upsert supposed to work also?
I have DSL defined tables (non IdTables)
insert works, but update totally confuses column names and the values to put into them.
The text was updated successfully, but these errors were encountered: