Landmaster
Landmaster

Reputation: 1053

Django Best Practices -- Migrating Data

I have a table with data that must be filled by users. Once this data is filled, the status changes to 'completed' (status is a field inside data).

My question is, is it good practice to create a table for data to be completed and another one with completed data? Or should I only make one table with both types of data, distinguished by the status?

Upvotes: 1

Views: 122

Answers (2)

djangomachine
djangomachine

Reputation: 699

One table is the optimal way to approach this particular case. Two tables requires you to enforce data integrity and consistency within your application, rather than relying on the power of your database, which is generally a very bad idea.

You should aim to normalize your database (within reason) and utilize the database's built-in constraints as much as possible to avoid erroneous data, including duplicates, redundancies, and other inconsistencies.

Here's a good write-up on several common database implementation problems. Number 4 covers your 2-table option pretty well.


If you do insist on using two tables (please don't), then at least be sure to use an artificial primary key (IE: a unique value that is NOT just the id) to help maintain integrity. There may be matching id integer values in each table that match, but there should only ever be one version of each artificial primary key value between the two tables. Again, though, this is not the recommended approach, and adds complexity in your application that you don't otherwise need.

Upvotes: 1

Not just Django

This is actually a very good general question, not necessarily specific to Django. But Django, through easy use of linked tables (ForeignKey, ManyToMany) is a good use case for One table.

One table, or group of tables

One table has some advantages:

  • No need to copy the data, just change the Status field.
  • If there are linked tables then they don't need to be copied
  • If you want to remove the original data (i.e., avoid keeping redundant data) then this avoids having to worry about deleting the linked data (and deleting it in the right sequence).
  • If the original add and the status change are potentially done by different processes then one table is much safer - i.e., marking the field "complete" twice is harmless but trying to delete/add a 2nd time can cause a lot of problems.

"or group of tables" is a key here. Django handles linked tables really well, so but doing all of this with two separate groups of linked tables gets messy, and easy to forget things when you change fields or data structures.

Upvotes: 1

Related Questions