0

I have made a table "Offenses" in my database which works fine, it has a primary key named "id"

Now i want to create another table with name offense_category. and want to create a foreign key that points to "id" in table "offenses"

however when i made migrations my "Offenses" table is being overwritten by offense_category table...

All the fields in offense are now gone. it shows the fields of offense_category under same name "Offenses" and no offense_category table is reflected in the database.

I am using mySQL with xampp server

models.py

from django.db import models
from django.contrib.auth.models import UserManager
from django.core.validators import int_list_validator


# Create your models here.

class Offenses(models.Model):
    id = models.IntegerField(primary_key=True)
    description = models.CharField(null=False, max_length=200)
    assigned_to = models.CharField(null=True, max_length=100)
    categories = models.TextField(null=True)
    category_count = models.IntegerField(null=True)
    policy_category_count = models.IntegerField(null=True)
    security_category_count = models.IntegerField(null=True)
    close_time = models.TimeField(null=True)
    closing_user = models.IntegerField(null=True)
    closing_reason_id = models.IntegerField(null=True)
    credibility = models.IntegerField(null=True)
    relevance = models.IntegerField(null=True)
    severity = models.IntegerField(null=True)
    magnitude = models.IntegerField(null=True)
    destination_networks = models.TextField(null=True)
    source_network = models.CharField(null=True, max_length=100)
    device_count = models.IntegerField(null=True)
    event_count = models.IntegerField(null=True)
    flow_count = models.IntegerField(null=True)
    inactive = models.BooleanField(null=True)
    last_updated_time = models.DateTimeField(null=True)
    local_destination_count = models.IntegerField(null=True)
    offense_source = models.IntegerField(null=True)
    offense_type = models.IntegerField(null=True)
    protected = models.BooleanField(null=True)
    follow_up = models.BooleanField(null=True)
    remote_destination_count = models.IntegerField(null=True)
    source_count = models.IntegerField(null=True)
    start_time = models.TimeField(null=True)
    status = models.CharField(null=True, max_length=100)
    username_count = models.IntegerField(null=True)
    source_address_ids = models.TextField(null=True)
    local_destination_address_ids = models.TextField(null=True)
    domain_id = models.IntegerField(null=True)

    objects = UserManager()
    class Meta:
        db_table = "Offenses"

    def __set__(self):
        return self.Offenses

class Offense_category(models.Model):
    Oid = models.ForeignKey(Offenses, on_delete=models.CASCADE)

    class Meta:
        db_table = "Offense_category"

    def __set__(self):
        return self.Offense_category

migrations

 class Migration(migrations.Migration):

    dependencies = [
        ('offense', '0008_auto_20190718_1107'),
    ]

    operations = [
        migrations.AlterField(
            model_name='offense_category',
            name='Oid',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='offense.Offenses'),
        ),
    ]
Kiran
  • 21
  • 5
  • Did you use the builtin migration command? Can you maybe post the migration script from the migrations folder? – Damien Baldy Jul 18 '19 at 06:53
  • I have made changs in the question you can see migration script there @DamienBaldy – Kiran Jul 18 '19 at 06:56
  • I'm not sure it there's anything related to your question but in your __str__ methods you're returning something else than a field. You should return a field (or a str) from the class. For example in your Offence model, something like: `self.description`. The same applies to Offence_category. `self.oid.description` – Higor Rossato Jul 18 '19 at 07:31

0 Answers0