3

I have django connected to SQL Server with django-pyodbc-azure. I am brand new to django. I created a model in django models.py, and I was able to migrate the model to SQL Server. All the proper tables are created.

I have historical data that will be eventually served up by the app. Is it possible to access an existing table that holds this historical data in the same DB? Or is it smarter to simply load the historical data into the new table that was created by django?

Conceptually, I would think it would be possible to make models (either through inspectdb or manually) for existing tables. However, adding those models would cause django to create new tables when migrations are performed, rather than recognizing that the tables already exist.

What is best practice? I think it might be cleaner to create all new tables, and then set up jobs in the agent to load historical data into those new tables.

OverflowingTheGlass
  • 1,949
  • 19
  • 55

1 Answers1

1

If you are 100% confident that you can describe the exact format and constraints of your historical table in a Django model, then you can try doing that and running makemigrations and then using the --fake-initial tag. This is the recommended process when you initialize a new application with an existing database.

Essentially you're telling Django what the table should look like, but not asking it to set up the table for you because it already exists. However, make sure Django has the right name for the table - customizing table names can be tricky.

However, if you have any doubt about setting up a model that perfectly describes your table, then create a new table and just load the old data into it. That would be cleaner because you know that Django has all the information it needs about the table. It also lets you apply new constraints to the incoming data if your old data isn't clean or could contain duplicates.

jgiuffrida
  • 91
  • 2
  • 4