0

I am using Django Framework in Python with MySQL Database

For adding a table in the database, I create a class in models.py and then run python manage.py makemigrations. This creates the migration file in my app with the table columns details.

Now, similarly, I want to create a new function in my database.

For now, I create a new empty migration file in my app using

python manage.py makemigrations --empty myApp

and then add the SQL code to the migration file

operations[
    migrations.RunSQL('Create function SQL Code')
]

This editing migration file can be done on a local or testing environment but is not advisable in the production environment. Is there any better way to create the MySQL function through Django Migrations command or models.py that works similarly to tables and creates functions only 1 time when we use the migrate command?

5
  • "This editing migration file can be done on a local or testing environment but is not advisable in the production environment" why? Creating custom migrations is a pretty established thing and I don't see any reason for it to be avoided for production environments. This is pretty much the way you are supposed to do it. Commented Apr 27, 2024 at 10:40
  • @AbdulAzizBarkat Thank you for the reply. But what if I have multiple servers, so won't it become a manual procedure to create custom migrations and edit the file on each of these servers? Commented Apr 29, 2024 at 9:36
  • Are you not committing your migrations to version control?... See: Should I be adding the Django migration files in the .gitignore file? Commented Apr 29, 2024 at 9:38
  • If you have multiple servers and aren't committing your migrations forget custom migrations you're simply going to have issues with the migration system itself. Commented Apr 29, 2024 at 9:39
  • @AbdulAzizBarkat Thank you for the reference link. We as a team were facing a lot of conflicts while committing migrations to version control, so recently we added that in git ignore and thus these custom migrations issues arouse. Commented Apr 29, 2024 at 13:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.