1

I have very strange situation here.

Problem: I describe original problem inside this post, but to sum up:

After using the project for a while, makemigrations stop working for me. (Yes I have set everything ok, setting are ok,.. please see my comments on previous post)

I decided that I can't figure this out, so I would start fresh. (thats why git is for :D )

So, I delete my django project, I create new virtual environment, and I also create new database and new user in postgres.

I update new database parameter inside my config file and try to run initial makemigrations but with no luch. It is still say that no new migrations are available.

I am desperate because I can't work on the project, so any solution would do :D

(virtual_environment) MacBook-Pro-2:MY_PROJECT marko$ python manage.py makemigrations connectors environment=local
I will use local settings.
No changes detected in app 'connectors'

All my migrations

(virtual_environment) MacBook-Pro-2:MY_PROJECT marko$ python manage.py showmigrations environment=local
I will use local settings.
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
connectors
 (no migrations)
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial

When lunch migration

(virtual_environment) MacBook-Pro-2:MY_PROJECT marko$ python manage.py migrate --fake-initial environment=local
I will use local settings.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.

The funny thing is that if I close postgres database, I still get the same text in terminal. I would guess that I should get some connection error.

database config yaml

database:
  port: 5432
  host: 'localhost'
  user: 'user1'
  password: 'user_password_123!'
  db: 'db1'

and my settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': CONFIG['database']['db'],
        'USER': CONFIG['database']['user'],
        'PASSWORD': CONFIG['database']['password'],
        'HOST': CONFIG['database']['host'],
        'PORT': CONFIG['database']['port'],
    }
}
Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
  • If `showmigrations` contains lots of `[X]`, that means you are connecting to an existing database, not a new one. – Alasdair Oct 18 '17 at 09:22
  • Post the commands you're using to create a new database. Django is reporting there's a database already set up and migrated there, indicating you're not at all creating a new database, or not connecting to the new database. – Adam Barnes Oct 18 '17 at 09:31

1 Answers1

0

After a lot of hours, I found solution, and it was actually my mistake. I will post the answer just for those who make similar mistake :D

In fact, @Alasdair and @AdamBarnes was right. I wasn't connecting to new database, but not because the database settings was wrong (host, port, username, db,...)

What I have in settings.py was settings for database and database for unit tests.

# Databases start
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': CONFIG['database']['db'],
        'USER': CONFIG['database']['user'],
        'PASSWORD': CONFIG['database']['password'],
        'HOST': CONFIG['database']['host'],
        'PORT': CONFIG['database']['port'],
     }
}

if 'test' or 'jenkins' in sys.argv:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test_database',
}

The problem was if statement below. For some reason he understand that this was always true. So program migrate sqlite internal test database.

I need to fix this with

if ('test' in sys.argv) or ('jenkins' in sys.argv):

This fix first problem, that when make migrations it leave empty database. After this fix, migrate was populate database with default Django tables, but not with my tables.

The fix for this was to import my models.py inside admin.py file.

import connectors.models

Thank's a lot, everybody that give me great hints, to find the solution.

Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97