Django Python unique=True Expression - python

I want to save Entries in my database such that I can delete them later. To identify them, i put the key attribute in my class Entry in models.py as shown below:
class Entry(models.Model):
name = models.CharField(max_length=200)
key = models.IntegerField(unique=True,default=0)
Every time I start the server, I will find the biggest key that is in Entry.objects.all().
Every time I create the first new Entry after starting the server, I want to take the key I found in the very beginning, increase it by 1 and then set this as the key for my new Entry. For every subsequent Entry, I will just take the key of the previous element, increase it by 1 and. set it as the key for my new Entry
However, before I do this, I want to know what Django considers as unique. For example, if i added three entries initially with keys 1, 2 and 3, and then I delete the last element with key 3. If I then restart the server, the biggest key I will find is 2 and the next Entry I will add will have key 2+1 which is equal to 3 again.
Is this key unique?
It was entered before but I deleted that element right? So is uniqueness determined by whatever I entered in the past or just depending on the elements currently in the database?
Also, does Django keep a track of all the instances of Entry that I added, so that it would somehow know if I added an Entry with key 3 before?
Note that my data is stored in a file called db.sqlite3
Thank you.

Seems like you are looking for something that already exists; models have an id field by default which is unique and monotonic (newer entries have bigger id).

None of this has anything to do with Django at all. This is pure database stuff: unique constraints are handled exclusively by the database, in your case SQLite, although the functionality is exactly the same for other DBs as well.
Of course, a unique constraint only takes into account rows that actually exist: what would be the point otherwise?
There are other things to bear in mind with your proposal as well. In particular, there is the issue of race conditions: if you do a query to get the maximum key value, and then create a new row with key+1, you run the risk of another user in the meantime adding their own row at key+1. Much better, as Iris and ryanageles suggest, to use the built-in primary key which is already automatically defined as the id field.

try to add primary_key=True in your key field.

Related

When updating unpredictable columns out of many, how to keep the other columns with their current values?

Using Python and the built-in Sqlite:
When I use a prepared statement, not knowing what columns the user will want to update:
UPDATE_AUTHOR = """UPDATE lastName=?, firstName=?, age=?, nationality=? FROM authors
WHERE _id = ?
"""
How can I replace the '?' with some value that will keep the current value for some of the columns?
From the user perspective, I will tell him for example to press 'Enter' if he wishes to keep the current value. So for instance, he presses 'Enter' on lastName, updates firstName, updates age, and presses 'Enter' on nationality. And I replace his 'Enter' with, hopefully, a value that will keep the current value.
Is that possible? If not, how can I solve this problem differently but efficiently?
I thought about building the prepared statement dynamically, in the above example: adding firstName=?, and age=?, after "UPDATE, and then the rest of the statement FROM authors WHERE _id = ?". But this seems less comfortable and less organized.
There are 2 ways of handling this question. One is to build a specific UPDATE query containing only the fields that will change. As you have said it is less comfortable because the query and the parameter list have to be tweaked.
Another way it to consistently update all the parameters, but keep the saved values for those which should not change. This is a common design in user interfaces:
the user is presented all the values for an object and can change some of them
if they confirm their choice, the application retrieves all the values, either changed or not and uses them in an UPDATE query.
Anyway, it is common the read the all the values before changing some, so it is not necessarily expensive. And at the database level, changing one or more values in an update has generally almost the same cost: a record is loaded from disk (or cache), some values are updated which is the cheapest operation, and it is then written back to disk. Even with the database caches, the most expensive part in the databases I know is to load and save the record.

Django autoincrement IntergerField by rule

I have this special case, where a customer requires a specific (legacy) format of booking numbers, the first one starts with the current year:
2015-12345
So basically every year I would have to start from 0
The other one is starting with a foreign-key:
7-123
So the first document created by every the user gets number 1, and so on.
Unfortunately there will be long lists starting with this booking number, so fetching all the records and calculating the booking number is not really an option. I have also thought about overriding the save() method, reading and auto-incrementing manually, but what about simultaneous inserts?
The best and most reliable way to do this is with a sql trigger That would completely eliminate the worries about simultaneous inserts. But overriding the save method is also perfectly workable.
Explicitly declare a primary key field and choose integer for it. In your save method if the primary key is None that means you are saving a new record, query the database to determine what should be the new primary key, asign it and save. Wherever you call your save method you would need to have a atomic transaction and retry the save if it fails.
BTW, you are starting for 0 each year. That's obviously going to be leading to conflicts. So you will have to prefix your primary key with the year and strip it out at the time you display it. (believe me you don't want to mess with composite primary keys in django)

Django: new database ids shifted?

I have created 31 objects in my database. Now, for some reason, if I create a new object through the Django admin page, the new object will have an id of 33. Now, suppose I change I change its id and then delete it. If I try to create a new object, it's id will be 34. So, the id is always shifted by 2. I'm very new to databases and Django, is there any reason for behaving like this? Thanks
Note: I didn't upload any code, because I don't think that's the problem...
By default, the id is an integer that is always incremented at the creation of an object. It is also incremented such that ids of deleted objects are never used again.
The incrementation is handled by the database itself, not Django. For example, with PostgreSQL, the corresponding database field corresponding the "id" has the "PRIMARY KEY" constraint. It basically means that the field should be not null, and with no duplicates. Moreover the field will be associated with a sequence, that stores the id to use for the next row creation. To change this number, run this in the database shell:
ALTER SEQUENCE yourobjectstable_id_seq RESTART WITH 1234;
However, as emphasized in the comments to your question, this is something you should not do: it is better to keep the "uniqueness" feature of the primary key, even for deleted objects, since other tables may use the id to refer to a row in your main table.

Auto Increment Field in Django/Python

I have a table in a django app where one of the fields is called Order (as in sort order) and is an integer. Every time a new record is entered the field auto increments itself to the next number. My issue is when a record is deleted I would like the other records to shift a number up and cant find anything that would recalculate all the records in the table and shift them a number up if a record is deleted.
For instance there are 5 records in the table where order numbers are 1, 2, 3, 4, and 5. Someone deleted record number 2 and now I would like numbers 3, 4, and 5 to move up to take the deleted number 2's place so the order numbers would now be 1, 2, 3, and 4. Is it possible with python, postgres and django?
Thanks in Advance!
You are going to have to implement that feature yourself, I doubt very much that a relational db will do that for you, and for good reason: it means updating a potentially large number of rows when one row is deleted.
Are you sure you need this? It could become expensive.
Here what I ended up using:
item.delete()
items = table.objects.order_by('order')
count =0
for element in items:
element.order = count
element.save()
count=count+1
You're probably better off leaving the values in the table alone and using a query to generate the numbering. You can use window functions to do this if you're up to writing some SQL.
SELECT
output_column,
...,
row_number() over (
order by
order_column)
FROM
TheTable;
Instead of deleting orders - you should create a field which is a boolean (call it whatever you like - for example, deleted) and set this field to 1 for "deleted" orders.
Messing with a serial field (which is what your auto-increment field is called in postgres) will lead to problems later; especially if you have foreign keys and relationships with tables.
Not only will it impact your database server's performance; it also will impact on your business as eventually you will have two orders floating around that have the same order number; even though you have "deleted" one from the database, the order number may already be referenced somewhere else - like in a receipt your printed for your customer.
You could try using signals post_save and post_delete to query the appropriate objects, sort them, and then look for missing numbers and reassign/save as necessary. This might be pretty heavy for a lot of data, but for only a few items that change rarely, it would be ok.
from django.db.models.signals import post_delete
from django.dispatch import receiver
def fix_order(sorted_objects):
#ensures that the given objects have sequential order values from 1 upwards
i = 1
for item in sorted_objects
if item.order != i:
item.order = i
item.save()
i += 1
#receiver(post_delete, sender=YourSortedModel)
def update_order_post_delete(sender, kwargs):
#get the sorted items you need
sort_items = YourSortedModel.objects.filter(....).order_by('order')
fix_order(sort_items)
I came across this looking for something else and wanted to point something out:
By storing the order in a field in the same table as your data, you lose data integrity, or if you index it things will get very complicated if you hit a conflict. In other words, it's very easy to have a bug (or something else) give you two 3's, a missing 4, and other weird things can happen. I inherited a project with a manual sort order that was critical to the application (there were other issues as well) and this was constantly an issue, with just 200-300 items.
The right way to handle a manual sort order is to have a separate table to manage it and sort with a join. This way your Order table will have exactly 10 entries with just it's PK (the order number) and a foreign key relationship to the ID of the items you want to sort. Deleted items just won't have a reference anymore.
You can continue to sort on delete similar to how you're doing it now, you'll just be updating the Order model's FK to list instead of iterating through and re-writing all your items. Much more efficient.
This will scale up to millions of manually sorted items easily. But rather than using auto-incremented ints, you would want to give each item a random order id in between the two items you want to place it between and keep plenty of space (few hundred thousand should do it) so you can arbitrarily re-sort them.
I see you mentioned that you've only got 10 rows here, but designing your architecture to scale well the first time, as a practice, will save you headaches down the road, and once you're in the habit of it, it won't really take you any more time.
Try to set the value with type sequence in postgres using pgadmin.

Is there a function to check whether an ID you want to use for an entity is available?

I think I read something about a function appengine has that can tell whether an ID / key you want to use for an entity is available, or if there was a function to get an available ID to choose. App engine team said also that we should set the ID when the entity is created and not change it. But in practice we can just copy everything to a new entity with the new ID?
Thanks!
Update
I think the function I'm looking for is allocateIDs from the docs:
http://code.google.com/appengine/docs/python/datastore/functions.html
To reserve one or more IDs, use allocate_ids(). To check whether an ID is already taken, just construct a Key for it using Key.from_path(kind, id) and try to db.get() it. Also note that IDs for keys with a parent are taken from separate pools and are only unique among keys with the same parent.
On the page describing transactions, a use case is presented where the entity in question, a SalesAccount is updated, or if the account doesn't exist, it is created instead. The technique is to just try to load the entity with the given key; and if it returns nothing, create it. It's important to do this inside a transaction to avoid the situation where two users are both racing for the same key, and both see that it doesn't exist (and both try to create it).

Categories

Resources