Question

Crear tablas y modificar estructura de otras en Postgrsql con migrate

Saludos. Despues de hacer un git pull, puedo con migrate crear mas tablas y actualizar la estructura de otras en postgresql. Trabajando con django.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
November 10, 2023

Greetings!

After performing a git pull, you can indeed use Django’s migrate command to create more tables and update the structure of existing ones in a PostgreSQL database.

When you git pull in a Django project, you’re typically pulling in changes that include updates to Django models. These model changes might be new models (which correspond to new tables in your database) or modifications to existing models (which might require changes to the structure of existing tables).

Here’s how the process typically works:

  1. Git Pull: First, you execute git pull to update your local codebase with the latest changes from the repository.

  2. Check for Model Changes: After pulling the changes, you should check if there are any changes in the Django models (located in the models.py files of your Django apps).

  3. Create Migrations: If there are model changes, you need to create new migration files. You do this by running python manage.py makemigrations. This command will generate migration scripts that represent the changes to your database schema.

  4. Apply Migrations: To apply these changes to your PostgreSQL database, you run python manage.py migrate. This command executes the migration scripts, thereby creating new tables or altering existing ones according to the changes in your models.

  5. Verify Database Schema: After running the migrations, your database schema should be updated. You can verify this by checking the database directly or using Django’s admin interface if it’s set up.

It’s important to ensure that the database your Django project connects to is accessible and correctly configured in your settings.py file. Also, make sure that your PostgreSQL user has the necessary permissions to create and modify tables.

Remember, the migrate command applies all pending migrations, not just the ones related to the latest git pull. So it’s a good practice to regularly pull changes and keep your database schema synchronized with your model definitions.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel