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!
Related
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.
I am having DJango migrations problem while making migration following error is coming.
When I run my applications using python manage.py runserver it shows this :-
However, running python manage.py makemigrations shows no changes detected
And Above three images are result after running python manage.py migrate.
What is the problem with this?
When the *table> already exists Error happens, it is usually due to deleting and rerunning the initial migration or models.py file. For these scenarios,
python manage.py makemigrations <app_name>
python manage.py migrate --fake-initial <app_name>
Or if you want to fake only one migration file
python manage.py migrate <migration_file_number> --fake <app_name>
--fake-initial tells Django to mark initial migration as migrated without actually running its corresponding SQL.
Django's migration document may be helpful
looks like you changed the database or migration files manually.
try to re-create the database.
delete the DB file
delete all migrations files (keep the init file)
run create migrations command
run migrate command
I am following this tutorial for learning how to create a Django (v2.0.1) app with multiple user types (teachers and students in this case). I cloned the associated code from the Github Repository, migrated the pre-made migrations and ran the site on localhost using:
python3 manage.py migrate
python3 manage.py runserver
The site works almost perfectly (Teacher sign up, login, quiz creation, and student log in are all in order). However, the student Signup fails with the following error:
OperationalError at /accounts/signup/student/
no such table: main.classroom_student__old
With the traceback pointing eventually to the file
django_school/classroom/forms.py, line 39:
student.interests.add(*self.cleaned_data.get('interests'))
That line comes from the definition of the following class in that forms.py file:
class StudentSignUpForm(UserCreationForm):
interests = forms.ModelMultipleChoiceField(
queryset=Subject.objects.all(),
widget=forms.CheckboxSelectMultiple,
required=True
)
class Meta(UserCreationForm.Meta):
model = User
#transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_student = True
user.save()
student = Student.objects.create(user=user)
student.interests.add(*self.cleaned_data.get('interests'))
return user
What I have tried:
Following answers to the many similar questions on this site, I assumed it was a migration issue so I tried running:
python manage.py migrate --run-syncdb
Operations to perform:
Synchronize unmigrated apps: crispy_forms, humanize, messages, staticfiles
Apply all migrations: auth, classroom, contenttypes, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
No migrations to apply.
But the error persisted. I then deleted db.sqlite3 as well as all migration files associated with the app classroom. I then ran python3 manage.py makemigrations followed by python manage.py migrate --run-syncdb, again to no avail.
This makes me think that this is an issue with the way the code is adding "interests" to the "student" user object. Indeed, commenting out the line in question stops the error and creates the new student user, however this has the obvious issue that the student has no interests stored.
Running python manage.py sqlmigrate classroom 0001 shows:
...
-- Add field quizzes to student
--
ALTER TABLE "classroom_student" RENAME TO "classroom_student__old";
CREATE TABLE "classroom_student" ("user_id" integer NOT NULL PRIMARY KEY REFERENCES "classroom_user" ("id") DEFERRABLE INITIALLY DEFERRED);
INSERT INTO "classroom_student" ("user_id") SELECT "user_id" FROM "classroom_student__old";
DROP TABLE "classroom_student__old";
COMMIT;
So the database in question (classroom_student__old) is getting created and deleted when another attribute "quizzes" is added to the student user object. Could this be creating the issue?
Instead of downgrading to sqlite 3.24.0, another solution for making the tutorial you listed run successfully is to change the requirements.txt file's django version to 2.2.7 before running the "pip install -r requirements.txt" command.
So, your requirements.txt file for this project would be:
Django==2.2.7
django-crispy-forms==1.7.0
pytz==2017.3
Just to close the question, #SwapnilBhate had the correct answer; it was the sqlite version 3.26.0. After downgrading to 3.24.0, deleting the directory, and reinstalling, everything works perfectly.
1.0) First try:
python manage.py migrate YOURAPPNAME --fake
2.0) If it still doesn't working delete all the migrations files inside your app migrations(except init.py) and pycache folder. After that run makemigrations and migrate.
Use django==2.2.7
then python manage.py migrate,
and finally python runserver
I am a newbie and a new user to Django and South. I created a new model (approval.py) in my models package (and linked it in model's init.py) and was able to successfully migrate my app to accept the new model. However, when I tried looking for that model on the admin site, I was getting a 403 forbidden error since the Guardian permissions were not applied.
So I directly dropped the table from sqlite command prompt and tried to perform the following commands again:
$python manage.py syncdb
...
(use ./manage.py migrate to migrate these)
$python manage.py schemamigration myapp --auto
Nothing seems to have changed.
$python manage.py migrate myapp
Running migrations for myapp:
- Nothing to migrate.
- Loading initial data for myapp.
Installed 0 object(s) from 0 fixture(s)
However, the table is no longer getting created, when I check in sqlite command prompt. Also, I had registered this model on my admin.sites.url, but it is no longer displaying it and giving error as:
DatabaseError at /admin/myapp/approval/
no such table: myapp_approval
Request Method: GET
Request URL: http://localhost/admin/approval/
Django Version: 1.5.1
Exception Type: DatabaseError
Exception Value:
no such table: myapp_approval
Exception Location: /home/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 362
Python Executable: /home/bin/python2.7
Python Version: 2.7.5
Python Path:
['.........']
Server time: Wed, 9 Jul 2014 19:12:00 -0500
I have already checked all the other questions posted on StackOverflow about using South for migrations, in particular the error about "Nothing seems to have changed". However I am not able to resolve my issue with any of those solutions.
I kindly request some help in either :
Creating the approval model again from scratch and migrating it successfully
Or Repairing the existing approval model.
Thanks.
Finally after a lot of trial and error, something very simple worked for me. I did not have much luck with the south migrations. So as I had manually dropped the table from sqlite3, I decided to manually create the table in sqlite3 and everything worked perfectly fine.
I used the below script on the sqlite3 prompt and it solved my issue and I was able to access the approval page correctly through my admin site and perform all CRUD operations on it.
CREATE TABLE "myapp_approval" ("id" integer NOT NULL PRIMARY KEY, "desc" varchar(256) NOT NULL);
Thanks everyone for your help and support!
You have already created a migration that creates the new table, and according to South's history management, you've run this migration. South does not know about the actual changes to your database.
What you need to do is to revert to a migration before the new table was created. If you look in your apps folder, you'll find a migrations folder with some migration files in it. If you created the new table in e.g. migration 0005, you need to (optionally) migrate backwards to 0005, and then you need to fake a backwards migration to 0004 and migrate again:
manage.py migrate myapp 0005
manage.py migrate myapp 0004 --fake
manage.py migrate myapp
I am trying to learn Django by setting up one of the projects I found on github. Afetr I ran the syncdb command it showed
Not synced (use migrations):
- django_extensions
- djangoratings
- profiles
- guardian
(use ./manage.py migrate to migrate these).
When I am running "python manage.py migrate app" , it gives
AttributeError: 'module' object has no attribute 'Migration'.
I also ran schemamigration app --auto and --initial as well. But nothing seems to be working. Can somebody point out where I am going wrong.
Are you actually running python manage.py migrate app exactly? If you want to migrate all apps, just run python manage.py migrate. (without app) If there is an app actually named app that you want to run migrations for, then you would do what you did.
If you still get the error after running python manage.py migrate, then there must be an invalid migration file somewhere. I would migrate each app individually like this:
python manage.py migrate django_extensions
python manage.py migrate djangoratings
... etc.
to find the app with bad a migration file. Once you find the app, look in the migrations folder of the app to find any empty migration files.
First thing you must run initial migration.
python manage.py schemamigration --initial
You are getting this error because of your project structure or there is some issue with your south package .try to remove and reinstalling south.
You need to see that there should not be any extra .py file in your migration folder.