Fernando Miranda
Fernando Miranda

Reputation: 9

Adding columns to an existing django table with a . postgreSQL db

I've been searching around for how to do this an di think i broke my table

I tried adding

dealership = models.ForeignKey('Employee', null=True)

To the field to the models.py, since I noticed thats where my other column fields were, and now the entire table is gone.

After some more research I saw that its supposed to be added to the migrations location and then run

$ python models.py migrations

Heres the model I want to add it to

## Gate access log - receives from RPi via API
##
class Eventlog(models.Model):
    ENTRY = 1
    EXIT = 2
    EVENT_CHOICES = (
        (ENTRY, 'Entry'),
        (EXIT, 'Exit'),
    )
    event = models.PositiveSmallIntegerField(choices=EVENT_CHOICES)
    employee = models.ForeignKey('Employee', null=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    status = models.BooleanField(default=True, help_text="Whether the employee was granted access")

    def __unicode__(self):
        return self.timestamp

    def __str__(self):
        return self.timestamp

and as the comment suggests, it pulls the from a raspberry pi through an api

My question is how do I properly add the column, the db already has the information for the column data I can't imagine it's that difficult to simply pull that info and how do I get my table back? The table seems to have vanished after I added to the models.py manually and when I tried undoing it just never came back.

Upvotes: 0

Views: 2220

Answers (2)

Devam Sanghvi
Devam Sanghvi

Reputation: 517

To add columns in postgresql db solution as below:

pip install django-extensions

and add this in the install app of settings.py

INSTALLED_APPS = [
    'django_extensions'
]

Now, run migrations for your installed apps

python manage.py makemigrations your_app_name
python manage.py migrtate your_app_name

Done! See Your Database...

Upvotes: 0

Walucas
Walucas

Reputation: 2568

You need to run two commands:

python manage.py makemigrations

then

python manage.py migrate

Upvotes: 1

Related Questions