Adding cloumns to an existing django table witha . postgreSQL db

Ive 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, 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 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
        (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

This is what migrations looks like in file

# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-06-16 07:19
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    initial = True

    dependencies = [

    operations = [
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('app_name', models.CharField(help_text='App Name', max_length=100)),
                ('app_title', models.CharField(help_text='App Title', max_length=100)),
                ('app_domain', models.CharField(help_text='App Domain', max_length=100)),
                ('app_url', models.CharField(help_text='App URL', max_length=254)),
                ('static_url', models.CharField(help_text='App Static Assets URL', max_length=254)),
                ('api_version', models.CharField(default='1.0', help_text='API version RPi should be using', max_length=4)),
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(help_text='Full name', max_length=100)),
                ('dealership', models.CharField(blank=True, default='', max_length=100, null=True)),
                ('code', models.CharField(help_text='32 char string', max_length=32)),
                ('license_plate', models.CharField(blank=True, default='', max_length=20, null=True)),
                ('active', models.BooleanField(default=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('modified', models.DateTimeField(auto_now=True)),
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('event', models.PositiveSmallIntegerField(choices=[(1, 'Entry'), (2, 'Exit')])),
                ('timestamp', models.DateTimeField(auto_now_add=True)),
                ('status', models.BooleanField(default=True, help_text='Whether the employee was granted access')),
                ('employee', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='core.Employee')),

My question is how do i properly add the column, the db already has the information for the column data i cant imagine its 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 manually and when i tried undoing it just never came back.

1 answer

  • answered 2018-03-13 22:39 Walucas

    You need to run two commands:

    python makemigrations


    python migrate