django 1.8.5 migrations fail for AbstractUser model - python

I'm having a weird problem when migrating my project from django 1.7.4 to 1.8.5
In my project I extend the basic User model like so:
App users:
class User(AbstractUser):
age = models.IntegerField()
def __unicode__(self):
return self.username
Now when migrating in django 1.8.5 for some reasons I have to start by doing
python manage.py makemigrations
Which will make the migrations for the users app.
If I do
python manage.py migrate
directly after this it fails with this error
django.db.utils.ProgrammingError: relation "users_user" does not exist
Then I do:
python manage.py migrate users
Which fails
"Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure
contenttypes is migrated before trying to migrate apps individually.
What is interesting is that even if this fails, now running
python manage.py migrate
Works
Operations to perform:
Synchronize unmigrated apps: messages, staticfiles, django_extensions, allauth, avatar, crispy_forms, debug_toolbar
Apply all migrations: sessions, users, contenttypes, admin, sites, account, auth, socialaccount
Synchronizing apps without migrations:
Creating tables...
Creating table avatar_avatar
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying account.0001_initial... OK
Applying account.0002_email_max_length... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK
Applying sites.0001_initial... OK
Applying sites.0002_set_site_domain_and_name... OK
Applying sites.0003_auto_20151104_1309... OK
Applying socialaccount.0001_initial... OK
Has anyone experienced the same problems when migrating from an older django version to 1.8?

contenttypes is migrated before trying to migrate apps individually.
All you need to do is add the dependencies so your migrations will run in order.
Have a look at django.contrib.contenttypes.migrations, add the latest one as a dependency in account.migrations and it should all work fine.

Related

Django and pgAdmin not aligned

I was trying to change the order of the table columns in a postgreSQL table. As I am just starting, it seemed easier to manage.py flush and create a new DB with new name and apply migrations.
I can see the new DB in pgAdmin received all the Django models migrations except my app model/table. I deleted all migrations folder (except 0001_initial.py) and still, when I run python manage.py makemigrations I get:
No changes detected
But the model class table is not in the DB. When I try to migrate the app model it says:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
Is there any way to delete all tables, makemigrations and migrate and postgres get all the tables? Any idea why postgreSQL/pgAdmin cannot get my Django app model/table?
You can do a particular app migration using below commands:
python manage.py makemigrations your_app_name
python manage.py migrate your_app_name
This works for me.
The app specific migrations in my case were not working.
python manage.py makemigrations your_app_name
python manage.py migrate your_app_name
What did the trick is to modify the models adding an additional fake variable, then all the variables (and not only the additional one) gets picked up by the makemigrations and migrate command. It is not a clever solution but it did the trick.

psycopg2.errors.UndefinedTable: relation "django_admin_log" does not exist

I just started learning Django, and I'm following a book as guide (the book is from August 2022, so new) and I ran into 2 problems.
The first one was that Python couldn't find the module psycopg2 which I then installed. Now I'm a little further and created my first model and migrated it to the database, which all seemed to work well. I then created a superuser and opened localhost:8000/admin/ and it sent me to the admin site, I logged in with my newly created user, so far so good. Now the problem.
This is what the site shows me:
And this is what the log says:
I've tried many approaches I found on here, for example deleted the migrations folder in my applications folder and then migrated my application again. I'll just go through a few other commands I've tried:
>> python manage.py migrate --run-syncdb admin
#CommandError: Can't use run_syncdb with app 'admin' as it has migrations.
>> python manage.py sqlmigrate admin 0001
# response: [The SQL query...]
>> python manage.py syncdb
# response: Unknown command: 'syncdb'
>> python manage.py migrate --fake
#Operations to perform:
# Apply all migrations: admin, auth, blog, contenttypes, sessions
#Running migrations:
# No migrations to apply.
This is what the database looks like right now
I found the answer myself on https://www.pythonanywhere.com/forums/topic/14632/
I first ran the command python manage.py migrate admin zero --fake, and then migrated again with python manage.py migrate, then ran the server and now the error is gone!

Django Heroku Migrate Error(?): "You have 21 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s)"

So I'm working on hosting a project and on my Heroku dashboard it says it's live, but every time I try to access it I get a 500 Server Error, even when trying to access it locally via Heroku CLI.
I tried running migrations because I get the following warning:
You have 21 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, recipe, sessions, users.
Run 'python manage.py migrate' to apply them.
Even after running makemigrations and migrate, as can be seen in screenshot below, any suggestions to what I might be missing?
C:\Users\davba\OneDrive\Documents\David\CSC-456\LIA_Project\Leftover-Ingredients->heroku run -a fathomless-cliffs-95117 python manage.py makemigrations
Running python manage.py makemigrations on ⬢ fathomless-cliffs-95117... up, run.3466 (Free)
No changes detected
C:\Users\davba\OneDrive\Documents\David\CSC-456\LIA_Project\Leftover-Ingredients->heroku run -a fathomless-cliffs-95117 python manage.py migrate
Running python manage.py migrate on ⬢ fathomless-cliffs-95117... up, run.3409 (Free)
Operations to perform:
Apply all migrations: admin, auth, contenttypes, recipe, sessions, users
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying recipe.0001_initial... OK
Applying sessions.0001_initial... OK
Applying users.0001_initial... OK
Applying users.0002_alter_profile_image... OK
C:\Users\davba\OneDrive\Documents\David\CSC-456\LIA_Project\Leftover-Ingredients->heroku run -a fathomless-cliffs-95117 python manage.py runserver
Running python manage.py runserver on ⬢ fathomless-cliffs-95117... up, run.6946 (Free)
Performing system checks...
System check identified no issues (0 silenced).
You have 21 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, recipe, sessions, users.
Run 'python manage.py migrate' to apply them.
November 17, 2021 - 00:20:55
Django version 3.2.8, using settings 'LeftoverIngredients.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

really broken django migrations

I get the following output when I run makemigrations and then migrate:
(roundwellenv) ruben#ruben-H81M-D2V:~/roundwell$ ./dev_migrations.sh
No changes detected in apps 'contenttypes', 'parents', 'admin', 'tips', 'tutors', 'login', 'auth', 'quiz', 'sessions'
Operations to perform:
Apply all migrations: admin, auth, contenttypes, login, parents, quiz, sessions, tips, tutors
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying login.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying parents.0001_initial... OK
Applying quiz.0001_initial... OK
Applying sessions.0001_initial... OK
Applying tips.0001_initial... OK
Applying tutors.0001_initial... OK
(roundwellenv) ruben#ruben-H81M-D2V:~/roundwell$ python manage.py createsuperuser
You have 21 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, login, parents, quiz, sessions, tips, tutors.
Run 'python manage.py migrate' to apply them.
I know something is really wrong because even if I delete pycache content and the conent from migration folders and a clean sqlite3 db it actually doesn't find any migrations it needs to apply unless I specify the apps I would like to create migrations for. Any clue as to what could be causing this? I suspect it might be my custom login app, but once the migrations are done (like they are on the system here the site is actually run) the DB works fine.
Try running the following, while in your project folder:
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
Then delete your sqlite DB, and try the migrations again.
Make sure you are in your project folder, or else this command will break your Django download!

python manage.py syncdb does not automatically load initial_data.json fixture in Python Django

I have Python Django project with app at apps/hello and a fixture with initial data at /apps/hello/fixtures/initial_data.json
When I git clone my Python Django project from github, checkout needed branch and run
./manage.py syncdb
it creates empty tables with no content i. e. does not load data from my initial_data fixture. Django asks me to register a superuser which is already registered in fixture.
Operations to perform:
Apply all migrations: admin, contenttypes, hello, auth, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying hello.0001_initial... OK
Applying hello.0002_auto_20141217_1326... OK
Applying hello.0003_auto_20141217_1329... OK
Applying sessions.0001_initial... OK
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): no
However I can steel load data manually
./manage.py loaddata apps/hello/fixtures/initial_data.json
Installed 12 object(s) from 1 fixture(s)
How to make Django do the same thing automatically on syncdb?
If you are using Django version 1.7 or later, you should probably know that automatic loading of fixtures has been deprecated:
If an application uses migrations, there is no automatic loading of fixtures. Since migrations will be required for applications in Django 2.0, this behavior is considered deprecated. If you want to load initial data for an app, consider doing it in a data migration.
If you're using the new built-in migrations, automatic loading of fixtures won't work.
I had the same problem, and I solved it by adding a few lines in my settings.py
FIXTURE_DIRS = (
os.path.join(BASE_DIR, 'hello/fixtures'),
)

Categories