Monday, October 29, 2018

Handling Database Tables without a unique column

I am currently leading a team handling a 16 year old database filled with junk.  I think it has existed since its HTML application was served by Oracle forms.
We are in production with Django, and turning off the more recent ColdFusion version this Thursday.

However, some of our ways of working reflect the deep SQL roots of the team.   For some tables, we have multi-column unique keys.

At the time I wrote these things, these seemed the best practices:
* For read-only tables, manufacture a unique column through concatenation in a corresponding database view.   Base the Django model on the database view.
* For read-write tables, do the work of adding a new unique ID column, but keep the table as managed = False.

Now that I am better at thinking in querysets, and can regularly use annotations, including F, Subquery and OuterRef expressions, I wonder whether there is a better way.   Has anyone ever tried doing this by adding to the SQL expressions code to manufacture a unique column in a QuerySet subclass or in the ModelManager?

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment