Prerequisite: Django Models
No such table? –
The class defined in
product/models.py is the mere idea of what our database is going to look like but it didn’t create any table in the database. We can assume class Phone as conceptual schema. Before the creation of any table, if we try to access the table before creation, it will throw an error like this.
OperationalError at /admin/product/phone/ no such table: product_phone
makemigrations command –
Python provides certain commands for user convenience so that without going into details of SQL,
a user can interact with the database. Now, we have created the class (conceptual schema of the database), we can use migrate command to create the actual schema in the database. Stop the server using CTRL+C if running and run the following command in the database.
python manage.py makemigrations
Above command will let the project know that we want to make changes in the database. You will see following quoting that Create model Phone.
what does this command do? This command will generate SQL statements that are supposed to be executed if we wish to make changes in the database.
If you want to see the generated commands, navigate to product/migrations/0001_initial.py. You will see file content
If you try to run the server now using command
python manage.py runserver
you will see
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): product. Run ‘python manage.py migrate’ to apply them.
As written in warning, run
python manage.py migrate
in your terminal. This will result in creation of table in database.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Connect Django Project to MongoDB using Django
- Styling Django Forms with django-crispy-forms
- Adding Tags Using Django-Taggit in Django Project
- Python | User groups with Custom permissions in Django
- Python | Django Admin Interface
- Python | Django-allauth setup and Configuration
- Python Django | Google authentication and Fetching mails from scratch
- Python | Uploading images in Django
- Intermediate fields in Django | Python
- Views In Django | Python
- Python | Sessions framework using django
- Python | Django News App
- Python | Relational fields in Django models
- Django URL patterns | Python
- Python | ToDo webapp using Django
- Django Sign Up and login with confirmation Email | Python
- Weather app using Django | Python
- Complete Django History | Python
- Django App Model - Python manage.py makemigrations command
- Django manage.py migrate command | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.