Django does not create tables

Asked

Viewed 164 times

2

Greetings to you all. I’m having trouble creating the tables for my apps by Django. I run the PYTHON MANAGE.PY MAKEMIGRATIONS command and it responds: NO CHANGES DETECTED. Then I run the PYTHON MANAGE.PY MIGRATE command and it responds: NO MIGRATIONS TO APPLY.

It’s just not creating the tables I need.

What I’ve already tried:

  • Delete Django tables in the database;
  • Erase the database and recreate it using Mysql Workbench.

My code in models.py:

class Cliente(models.Model):
    nome     = models.CharField(max_length=100, unique=True)
    slug_c   = models.SlugField(blank=True, unique=True)
    cpf_cnpj = models.CharField(verbose_name="CPF/CNPJ", max_length=18, unique=True)
    email    = models.EmailField(unique=True)
    fone     = models.CharField(verbose_name="Telefone fixo", max_length=25, blank=True)
    celular  = models.CharField(max_length=25)
    endereco = models.CharField(verbose_name="Endereço", max_length=200, blank=True)
    numero   = models.CharField(verbose_name="nº", max_length=7, blank=True)
    compl    = models.CharField(verbose_name="Complemento", max_length=50, blank=True)
    bairro   = models.CharField(max_length=25, blank=True)
    cidade   = models.CharField(max_length=50, blank=True)
    estado   = models.CharField(max_length=2, blank=True)
    cep      = models.CharField(max_length=10, blank=True)
    objects = ClienteMan()
    class Meta:
        verbose_name_plural = "Clientes"
    def __str__(self):
        return self.nome

class Obra(models.Model):
    nomeid    = models.ForeignKey(Cliente, to_field="nome", verbose_name="ID Cliente", on_delete=models.DO_NOTHING)
    titulo    = models.CharField(verbose_name="Título", max_length=200, unique=True)
    slug_o    = models.SlugField(blank=True, unique=True)
    tipo_obra = models.CharField(verbose_name="Tipo de obra", max_length=50)
    endereco  = models.CharField(verbose_name="Endereço", max_length=200)
    numero    = models.CharField(verbose_name="nº", max_length=7)
    compl     = models.CharField(verbose_name="Complemento", max_length=50, blank=True)
    bairro    = models.CharField(max_length=25)
    cidade    = models.CharField(max_length=50)
    estado    = models.CharField(max_length=2)
    cep       = models.CharField(max_length=10)
    obs       = models.TextField(verbose_name="observações", blank=True)
    objects = ObraMan()
    class Meta:
        verbose_name_plural = "Obras"
    def __str__(self):
        return self.titulo

class Diario(models.Model):
    obraid    = models.ForeignKey(Obra, verbose_name="Obra", on_delete=models.DO_NOTHING)
    data      = models.DateTimeField(blank=False, null=False)
    slug_d    = models.SlugField(blank=True, unique=True)
    imagem    = models.ImageField(upload_to='diarios/', null=True, blank=True)
    arquivo   = models.FileField(upload_to='diarios/', null=True, blank=True)
    descricao = models.TextField(verbose_name="descrição", blank=True)
    objects = DiarioMan()
    class Meta:
        verbose_name = "Diário"
    def __str__(self):
        return self.data.strftime('%d/%m/%Y')

Thanks so much for your help.

  • To resolve this issue, remove the contents from the folder migrations leaving only the file __init__.py. For each of the apps that is having this problem. Do not run this procedure in production.

  • Briefcase Migrations application is empty. I tried to run the commands again and the problem persists.

  • That’s the problem, you have to have the file __init__.py

  • IT WORKED!!! Thank you very much plea your help! I created a blank file with the name of __init__.py inside the briefcase migrations from my app and rotated the makemigrations commands again and then migrate. It worked perfectly.

2 answers

0


In this solution I am assuming that there is no problem in erasing the database, in this way a way to solve the problem is:

  • remove the contents of the folder migrations that is inside each app, leaving only the file __init__.py

  • If there are no files inside the folder migrations you only need to create the file __init__.py

  • If you use a Unix-based OS, it is in the folder migrations, just the command :

      touch __init__.py
    

If you have many apps in your project the same effect can be achieved by being in the same location as the Manage.py file and running the following command:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

After performing these steps just drop the database or delete the file: db.sqlite3.

It will now be possible to generate a new Migrations and apply the changes to the created Migrations using the command:

python manage.py makemigrations
python manage.py migrate

For other methods, simply refer to tutorial by Victor Freitas

0

If you previously made a makemigrations and then migrate it inside the folder that has the Client model to another folder called "Migrations"

A pasta migrations: 
Deve estar com arquivos python chamado 
0001_initial.py
0002_initial.py 
0003_initial.py  esses são como um referencia de modificação para comparação 
de migrations.
Python pode não ter reconhecido mudanças usando como referencia esses 
arquivos gerados.
Pode apagar todos exceto o arquivo python chamado "__init__.py"
E utilizar novamente a instrução makemigrations e depois o migrate.
Supondo que já excluio o banco de dados e as instruções não modificou
o que queria.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.