question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wasting Primary Key ID's

See original GitHub issue

In case if you try to import a CSV file using ImportExportModelAdmin

say I have a Django modes named PRODUCTS as:

product_id (pk)     name (char)         size (char)   etc...
1                           milk                     500ml
2                           bread                  Large

admin.py looks like:

class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource

admin.site.register(Product, ProductAdmin)

resource.py that holds ProductResouces class look like:

class ProductResource(resources.ModelResource):
    class Meta:
        model = Product
        import_id_fields = ['product_id']
        skip_unchanged = True
        report_skipped = True
        use_transactions = True

and the issue is with uploading CSV file without product_id:

the CSV file looks like:

product_id (pk)     name (char)         size (char)   etc...
                            fresh milk              500ml

Note: Here I haven’t passed the product_id. So when I do import from my Django admin, it shows data as:

product_id (pk)     name (char)         size (char)   etc...
3                           fresh milk             500ml

Probably it get this primary key returned from database (MYSQL) and then shows confirm import button on chick of which the record I have is: 4 fresh milk 500ml

the id imported is 4 rather than 3.

I have IMPORT_EXPORT_USE_TRANSACTIONS = True in settings.py also have tried to make use_transactiosn = False in both settings.py and in resources.py and alternative combinations, still it wastes 1 primary key id in database whenever I don’t pass a primary key in my csv file

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewgy8commented, Feb 26, 2019

Relevant bit of text here from #560 :

These only ever increase, and they’re exempt from the usual transaction rollback rules to permit multiple transactions to grab new IDs at the same time. This means that if a transaction rolls back, those IDs are “thrown away”; there’s no list of “free” IDs kept, just the current ID counter. Sequences are also usually incremented if the database shuts down uncleanly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it ever a good idea to not have an 'id' primary key for a table?
Longer answer: if you have a table to be used in a many-to-many relationship, you don't really need a primary key. Then it...
Read more >
Is it good practice to always have an autoincrement integer ...
On larger systems, it can be worth ignoring minor benefits of those individual primary keys and consistently use integer autoincrement ID in ......
Read more >
Obfuscating your Primary Keys | Variables and Observations
Some commenters have the mistaken belief that exposing the primary key of one of their database tables is a security breach.
Read more >
Auto-generated primary keys: UUID, serial or identity column?
This article explores the old question what to use for autogenerated primary keys: UUID, serial or identity column?
Read more >
Unique identifier as primary key - Kohera
– If you use them as Clustered Primary key, they do cause fragmentation because the insertion point of a new record is dictated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found