Python social auth in Django, makemigrations detects no changes - python

I was following the documentation to get python social auth on my django project
https://python-social-auth.readthedocs.org/en/latest/configuration/django.html
And after adding 'social.apps.django_app.default', to the INSTALLED_APPS in my settings.py I run this:
python manage.py makemigrations
I get this
No changes detected
Shouldn't this command be doing something. Because without this I can't migrate to create the tables that are needed for the auth.
EDIT:
I've also tried this command, and still ended up getting the same result
python manage.py makemigrations main
where 'main' is the name of my app

Today i ran into this problem. The error is in the documentation itself.
You should run $ python manage.py migrate directly. It creates tables in the database.
All the old tutorials used makemigrations, I think it was used in earlier versions of django.

My answer will be cover some basics so one can understand this types of errors easily.
Let me clear some basic terminology about migrations in the latest versions of Django(From 1.7 to under development ones).
In older versions of Django when One has to make changes in Models (Eventually in Database) then One has to use South app to apply changes in Database without affecting older database.
Django developer community has included this south app in and after Django 1.7 and also provided some simple commands to apply migrations.
When one install New app(Above question scenario) or one make changes in existing models and wish to apply changes in database then one has to tell database about what changes they want to make. To do so one has to make migrations and below is the command.
$ python manage.py makemigrations app_name
or If it is initial then no need to specify app_name, it will consider all apps.
This command will generate migrations files which will include instructions for database to make what tables and what are the attributes of that table and what will be the relationships between tables and what are changes in the current tables etc etc.
Now one has to run below command to apply this all changes in database.
$ python manage.py migrate app_name
or If it is initial then no need to specify app_name.
Please shoot any questions If you have any and please take a look at Django's official documentation on migrations for more information.

The possible reason is your project doesn't use any database models of 'social' application yet. Add a URL in your urls.py, link it to 'social' urls.

to Django < 1.8
INSTALLED_APPS ['social.apps.Django_appConfig',]

Related

Create Django app with no User model - 3.1.3 wants to migrate auth_user?

I've got a Django application which has been happily humming along now for quite some time (2 years or so).
It's on 3.0.10 currently - when I tried to upgrade to 3.1.3, it says there was a migration for the auth application. No worries! Never been an issue before...
I ran python manage.py migrate and got the following error:
"Cannot find the object "auth_user" because it does not exist or you do not have permissions."
Which, I suppose would be true because we do not have a User model at all in this application. There is no auth_user table, that is correct. In other apps we have an AbstractUser that routes to a table named: org_user - but again:
this particular app (and project) do not have any User model associated with them
Obviously this (apparently, now) leads to some issues.
Any thoughts on how to get around this? I thought about removing auth from installed apps and tried that but it led to more issues when trying to runserver.
You can fake a migration using --fake option:
python manage.py migrate --fake auth
python manage.py migrate
But I suspect the error you get could be symptomatic of a project design issue, such as not setting properly settings.AUTH_USER_MODEL
Solution 1 :- Run these commands :-
python manage.py migrate auth
python manage.py migrate
Solution 2 :- Try to copy your Project in another folder and then run migrations.

Uninstalling django-tinymce. Import error when makemigrations

Thanks for taking the time to read this.
I was not happy with TinyMCE extension in django and decided to switch to django-summernote. I first ran
pip uninstall django-tinymce
Removed all mentions of tinymce in the actual project.
Followed the instruction to install django-summernote. Upon completion I decided to run
python manage.py makemigrations
python manage.py migrate
to apply new extension, but get an error:
File "/Users/rasulkireev/Sites/dj-pw/now/migrations/0006_auto_20190703_0134.py", line 4, in <module>
import tinymce.models
ModuleNotFoundError: No module named 'tinymce'
I'm not sure why would Django care about what I did previously since I am simply asking to replace the editor. I can't run python manage.py runserver either.
I can't do anything before fixing this. I beg you guys, please help.
Note: I would ideally want to keep the contents of my current database.
Thanks for taking the time to read this.
Once you have removed all the mentions of tinymce from the models, admins and other files, you need to migrate using a special "flag":
python manage.py makemigrations
python manage.py migrate --fake-initial
I am not to clear on the backend of this method, but it works. You can read more about it the official Django website:
https://docs.djangoproject.com/en/2.2/topics/migrations/
This will make a new initial migration for your app. Now, run python
manage.py migrate --fake-initial, and Django will detect that you have
an initial migration and that the tables it wants to create already
exist, and will mark the migration as already applied. (Without the
migrate --fake-initial flag, the command would error out because the
tables it wants to create already exist.)
Note that this only works given two things:
You have not changed your models since you made their tables. For
migrations to work, you must make the initial migration first and
then make changes, as Django compares changes against migration files, not the database.
You have not manually edited your database - Django won’t be able to detect that your database doesn’t match your models, you’ll just get errors when migrations try to modify those tables.

Database Api Python/django

I am working on a tutorial on Django
When I invoke the interactive shell to access the database API using the command python manage.py shell, I receive the following prompt
In [1]:from music.models import Album, Song
In [2]: Album.objects.all()
Then the next line gives me this
OperationalError: no such table: music_album
Like the error says, it means that your database has no tables that correspond to these models.
Typically that means that you forgot to migrate your database. By running:
python manage.py makemigrations
Django will construct files (in someapp/migrations/) these files describe the steps that have to be carried out such that tables are constructed with the correct columns, or later in the progress columns can be renamed, tables removed, new tables constructed. If you run the command, you will see new files popping up that describe these migrations.
But now you still did not migrate the database itself, you only constructed the files. It is for example possible that you want to add custom migrations yourself. In case the migration files are correct, you can run
python manage.py migrate
to change the database accordingly.
In order to run the migrations, the database has to be specified correctly in the settings.py file. But since you obtain an OperationalError that complains about the table not being found, I think this has already been handled (correctly).
See the Django topic on migrations [Django-doc] for more information.
Configure your database connection in your settings.py and, after creating Django models, you have to execute two commands:
python manage.py makemigrations
python manage.py migrate
Then tables will be created in the database and you will be able to use then in the shell

Switching database backend in Mezzanine

I am trying to script a series of examples where the reader incrementally builds a web application. The first stage takes place with Mezzanine's default configuration, using built-in SQLlite:
sudo pip install mezzanine
sudo -u mezzanine python manage.py createdb
After the initial examples are complete, I want to switch the existing setup to a mysql backend. If that is too complex, I at least want to re-create the built-in examples that come with Mezzanine on the new backend, but Mezzanine won't allow re-running createdb
CommandError: Database already created, you probably want the migrate command
This seems like something that should be incredibly simple, yet I can't seem to get it quite right (and migrate alone does not do the trick). Google and official docs not helping either.
Steps I am taking: first, I create a MySQL database on Amazon RDS. Then, I set appropriate configuration for it in myapp/local_settings (I am sure these steps are correct). Then:
sudo apt install python-mysqldb
sudo -u mezzanine python /srv/mezzanine/manage.py migrate
but then:
Running migrations:
No migrations to apply.
What am I missing?
The Mezzanine project is based on Django, the Python framework.
Unless you encounter a Mezzanine specific problem, most issues can be solved by figuring out how its done the Django way.
Migrations is just Django's way of refering to alterations & amendments within the DB, ie, the schema (because apps evolve & databases are metamorphic).
In order to actually migrate the data however you could:
Export the contents from the current database, eg:
./manage.py dumpdata > db-dump-in-json.json
./manage.py --format=xml > db-dump-in-xml.xml
This may crash if there is too much data or not enough memory. Then the thing would be to use native DB tools to get the dump.
Create and add the settings for the new DB in settings.py:
Create the tables and set them up (based on your models) on the newly defined DB:
./manage.py makemigrations
./manage.py migrate
createdb = syncdb (create) + migrate (set) combined
And reload the exported data there:
./manage.py loaddata db-dump-in-json.json

django 1.7 how to migration with existed tables

I upgrade to django 1.7
and need to deal with migration
My database already exists tables
Here is my step :
1.I delete the migration in django app test
2.I remove south(from INSTALLED APPS ) in django settings.py
3.run python manage.py makemigrations test
Migrations for 'test':
0001_initial.py:
- Create model Person
- Create model Book
- Create model Artical
run python manage.py migrate test
Operations to perform:
Apply all migrations: test
Running migrations:
Applying test.0001_initial... FAKED
is it right???
How to do migration if I already have tables?
Please guide me Thank you
it is right if the tables that exist in your database have the same structure as your current models (i.e. you haven't modified your models.py file after you applied you last migrations with South). If that's the case, you can proceed without worries.
If the structure is different, you can try different approaches:
Create the needed migrations manually. You will have to write your own migration with the needed modifications. Here you can find some example code of a migrations file for altering or adding fields and here you have the complete reference of the methods you can call
Modify your database manually with phpmyadmin, raw sql or the method you prefer.
Do a python manage.py dumpdata app1 app2 app3 > data.json, remove the db, create it again and to a python manage.py loaddata data.json. This may raise some errors because the data you saved was with the old structure, you will have to deal with each error.
Or the least preferred, if the data you had is not needed, just remove the db and recreate it.
There is nothing wrong with the way you are doing it.
The reason the first migration is faked is because your tables are already created:
Run python manage.py migrate. Django will see that the tables for the
initial migrations already exist and mark them as applied without
running them.
From the documentation on upgrading from South.

Categories

Resources