Skip to content

Conversation

@bbzylstra
Copy link

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.

@netlify
Copy link

netlify bot commented Nov 19, 2025

Deploy Preview for inventree-web-pui-preview ready!

Name Link
🔨 Latest commit f17a11e
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/691f5fe2fe41cd00075a700c
😎 Deploy Preview https://deploy-preview-10862--inventree-web-pui-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 93 (🔴 down 1 from production)
Accessibility: 81 (no change from production)
Best Practices: 100 (no change from production)
SEO: 78 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@SchrodingersGat SchrodingersGat added bug Identifies a bug which needs to be addressed import / export Data importing, exporting and processing backport Apply this label to a PR to enable auto-backport action backport-to-1.1.x labels Nov 19, 2025
@SchrodingersGat SchrodingersGat added this to the 1.2.0 milestone Nov 19, 2025
Copilot finished reviewing on behalf of SchrodingersGat November 19, 2025 11:06
Copy link
Contributor

Copilot AI left a 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.

bbzylstra and others added 2 commits November 19, 2025 10:11
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]>
@bbzylstra
Copy link
Author

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.

@SchrodingersGat
Copy link
Member

@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?

@matmair
Copy link
Contributor

matmair commented Nov 19, 2025

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

@bbzylstra
Copy link
Author

@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.

@bbzylstra
Copy link
Author

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:

@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    """Create or update user profile when user is saved."""
    # Disable profile creation if importing data from file
    if isImportingData():
        return

    if created:
        UserProfile.objects.create(user=instance)

    try:
        instance.profile.save()
    except User.profile.RelatedObjectDoesNotExist:
        UserProfile.objects.create(user=instance)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Apply this label to a PR to enable auto-backport action backport-to-1.1.x bug Identifies a bug which needs to be addressed import / export Data importing, exporting and processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Updating database version

3 participants