Adding stored procedures

172 views
Skip to first unread message

Chris Wedgwood

unread,
Apr 18, 2018, 2:50:52 AM4/18/18
to django...@googlegroups.com
Hi All

I am using SQL SERVER in my latest django project and I am going to be using some stored procedures.

My question is about how to go about deploying stored procedure changes with django. I have looked through the migrations documentation which looks very specific to model changes.

Has anyone had experience of having to create other things such a stored procedures/views/functions?

thanks
Chris




Matthew Pava

unread,
Apr 18, 2018, 8:57:10 AM4/18/18
to django...@googlegroups.com

Hi Chris,

SQL Server is not one of the databases that Django supports out of the box.  There are third party packages available, though, but I haven’t tested any of them with recent versions.

Saying that, you can create views in your database backend.  In your migrations file, use the RunSQL operation.

https://docs.djangoproject.com/en/2.0/ref/migration-operations/#runsql

 

In your models file, change the Meta option, managed, to False.

https://docs.djangoproject.com/en/2.0/ref/models/options/#managed

 

Good luck!

 

As for stored procedures and functions, I haven’t worked enough with those mixed in with Django to give you much guidance except to do some more searching on the web.  And I would also advise against using them so that you can keep your code in your Django project.  I haven’t found a need for them myself while using Django.

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACQBJYU5CDhCrP7cNRpzp7fRDr1pyf48css-EaVW%3Drj65SmEsg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Chris Wedgwood

unread,
Apr 19, 2018, 10:01:33 AM4/19/18
to Django users
Thanks Matthew

I probably need to think about this some more

I think using runsql will work. Do you know if you can set migrations to be rerunnable? It would be useful to be able to change something like a stored procedure and then it gets dropped and recreated each deployment

Saying that the stored procedure is going to be used for an import task that isn't actually related to Django so probably needs to be deployed  by another mechanism and I should leave migrations for only DJango specific changes

thanks
Chris

Matthew Pava

unread,
Apr 19, 2018, 10:07:50 AM4/19/18
to django...@googlegroups.com

Hi Chris,

The migrations will be run whenever you execute the migrate command.  When I want to make changes to an unmanaged model, in the next migration file I add a DROP statement to RunSQL, and then the new code to generate the VIEW.  I haven’t attempted reversing a migration, though.  I try to test with the new migration before deploying to production.

Best wishes,

Matthew

--

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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Jani Tiainen

unread,
Apr 19, 2018, 3:24:39 PM4/19/18
to django...@googlegroups.com
Hi. 

Django migrations are run only once. If you want to change you stored procedure you always need a new migration.

In case of being last migration you could develop it by having drop clause im reverse migration. But if there is another migration(s) between current and new stored procedure you need to create new migration.

One option could be to hook pre- or post migration signal and apply your stored procedure there.


Larry Martell

unread,
Apr 20, 2018, 12:47:46 AM4/20/18
to django...@googlegroups.com
There are django-mssql and django_pyodbc but I was never able to get
either of those to work for me. I ended up using odbc - I couldn't use
the ORM, but I still was able to connect to the MSSQL DB from my
django app using raw queries.
> https://groups.google.com/d/msgid/django-users/58f5f935832647b0afd58ac5105b45f0%40ISS1.ISS.LOCAL.
Reply all
Reply to author
Forward
0 new messages