-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Bug] Import-records fix when importing from older InvenTree+Postgres version #10862
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
base: master
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for inventree-web-pui-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug where importing data from older InvenTree+Postgres versions fails due to primary key misalignment between user profiles and user accounts. The fix ensures user profile primary keys match their corresponding user account primary keys during the import process.
Key changes:
- Added logic to track user primary keys during data preprocessing
- Implemented pk reordering for user profiles to align with their associated user accounts
- Updated comment to reflect the dual purpose of the user data processing section
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Changed .startswith to exact matching for users.userprofile. Co-authored-by: Copilot <[email protected]>
Added validation checks to user primary key pairing dict. Co-authored-by: Copilot <[email protected]>
|
Comments look good, I tested them with the broken export and everything is still working as intended. If you think the primary key reordering is okay then I think it's ready (I have another version that turns off user profile creation + profile checks during data import). If you want to do further testing I can provide my export.json (with PII stripped) that was originally failing the import-records. Or you can generate one yourself by changing the order of primary keys in the user.profile sections of the inventree test data. |
|
@matmair I'd like your input on this - would you prefer the approach implemented here by @bbzylstra or to disable user profile creation entirely on data import? |
|
Not sure I like this approach, introducing any more custom behavior in the importer is dangerous. I would prefer to handle it in the signal and maybe the api (UserProfileDetail) to create the profile if it is missing |
|
@matmair @SchrodingersGat I reverted tasks.py and updated users/models.py to disable the two validators that cause the upload to fail, and tested that it is working. I am holding off on handling what to do if user profiles are missing, perhaps the importer should verify that the db is in a valid state (1-to-1 pairing between user accounts and profiles) after the upload is finished? Conversely, maybe the importer should verify there is a 1-to-1 pairing between user profiles and accounts before importing the data, and if not shut down with a warning to the user. An export in this state would imply there was corruption/failure during export. |
|
Another option is to just wrap instance.profile.save() inside of create_or_update_user_profile with an error handler for User.profile.RelatedObjectDoesNotExist like this: Tested that this works when a corrupted export does not include user profiles for all imported accounts. The user profile will be created on next sign-in from that user. |

Fixes #10780 where exports from older versions of InvenTree+Postgres cause the import-records task to fail when importing user profiles.
User profiles are created with a 1-to-1 mapping to user accounts via a signal during account creation. When the import-records task is run, user accounts are imported first into the database, so by the time the import-records tool begins importing user profiles there will already be a set of (signal-created) user profiles in the db.
The 1-to-1 constraint will fail if the primary key of the user profile does not line up with the primary key of the user account (for example, the dump has user profile pk: 1 set to user: admin, but in the db user profile pk: 3 is set to admin, when import-records imports user profile pk:1, the 1-to-1 constraint is violated since 2 user profiles (pk:1 and pk: 3) share the admin user).
The proposed fix is to reorder the primary keys of the user profiles to match that of the user accounts (so if admin is pk: 3, the user profile corresponding to admin will have its pk set to 3). How user profiles are created must have changed in a recent version of InvenTree as these seem to enforce this pairing in the db exports.
I have another version that fixes this by turning off the automatic creation of user profiles during import-records as discussed in the bug report, but it requires turning off the other user/group validators as well. I can PR that version if reordering the user profile primary keys (non-destructively in a temp-file) is an issue.