So, I am learning Django using Django By Example book.
I moved to a new chapter where I needed to make a new project and app. This is what I did.
django-admin startproject myshop
cd to myshop directory
django-admin startapp shop
python manage.py migrate
I am unable to migrate with the following error:
django.db.migrations.exceptions.NodeNotFoundError: Migration django.db.migrations.exceptions.NodeNotFoundError: Migration auth.0009_user_following dependencies reference nonexistent parent node (u'account', u'0002_contact')
I did make Contact model in account app in my last project.
How is the new project related to the old one?
Please help me resolve this problem. Thanks.
It means that you don't have the file 0002_contact in your account app's migration folder. You need to recover the file or configure this file 0009_user_following and remove the (u'account', u'0002_contact') line
Related
Him I am trying to set up my first django project. I have never done Django before so I don't know anything about it. I am trying to follow the official Django tutorial page, it tells me to open views.py. I can't find it anywhere in my "mysite" folder. In it, I have manage.py , db.sqlite3, __init__.py, settings.py, urls.py and wsgi.py along with the pycache folder, but no signs of views.py. Can anyone help me?
Running this command in console django-admin startproject myfirstproject will create a new django project for you, which will have another sub-folder named same as your project name. By default on creating a new project, the views.py file isn't created you have to create it manually in your project sub-folder.
To create a new app, Run django-admin startapp myapp which will create a new sub-folder under your parent directory which will have views.py where you can write your own custom views for your HTML templates
I know there are many questions about this problem, I looked through the solutions and unfortunately none of them worked for me.
I created a new app called "usermanagement", and added a model to the app. After adding the model I added usermanagement to INSTALLED_APPS in settings. Then I ran python manage.py makemigrations, and python manage.py migrate. This all worked fine! I also did try running the migrations with the app-name.
The problems start when I try to add a new instance of the model to the database in the Python-Django shell, by using:
>>>a = ClubOfficial(name="randomName", email="randomemail#random.com")
>>>a.save()
I get the following error:
django.db.utils.ProgrammingError: relation
"usermanagement_clubofficial" does not exist
LINE 1: INSERT INTO "usermanagement_clubofficial" ("name", "email") ...
Below is the model code:
class ClubOfficial(models.Model):
name = models.CharField(max_length=254)
email = models.EmailField(max_length=254)
If it helps, I use postgresql, and have tried restarting the server. The other apps in the program also work perfectly fine, it is just usermanagemenet that has this problem.
Does anyone know what could be going wrong?
Thank you for your time!
Note: I created a new app now with a different name, copy-pasted things from usermanagement and everything worked fine. I think the problem might be that before there was an app named usermanagement which was deleted, before I created it again. Maybe that messed up the database somehow.
TL;DR: Make sure your app's migrations folder has an __init__.py file. If it isn't there, create it again as an empty file.
I ran into this. In my case I had a previously working django app, not yet moved to production, so I deleted everything in my app's migrations folder, then using django extensions I wiped the postgresql database and cached files with:
./manage.py clear_cache
./manage.py clean_pyc
./manage.py reset_schema
./manage.py reset_db
# then deleted all files (including __init__.py) from my app's migrations folder.
I verified that my postgresql database had no tables. I then ran:
./manage.py makemigrations
./manage.py migrate
Which gave the output:
No changes detected
./manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
(about 11 more lines of output here which are similar)
It is notable that my model's names where nowhere in the migration. I printed the tables in my postgresql database and the Widget and Section tables were both missing. Running the app gave me this error (I substituted 'app' and 'model' for their real names):
ProgrammingError at /my_path
relation "app_model" does not exist
LINE 1: ..."."my_field", "app_model"."my_field" FROM "appname...
So the tables for the models were not being created in the database. My app's migrations folder was also completely empty. The solution was to just add an empty __init__.py to my app's migrations folder, and after that running makemigrations was able to create and save the migration file in the folder. This could then be executed with migrate. You can test this for yourself by running makemigrations with and without the __init__.py in the migrations/ folder.
This solution was not mine but one given by user Ljubitel on another post but it was not the accepted answer there, but the accepted answer didn't work for me so I wrote this solution here to hopefully help others.
I had this same problem, but all I had to do was run
$ python manage.py makemigrations
and
$ python manage.py migrate
In case you have deleted your migration folder.
Create a folder called migration in whatever app name you have created and then create a file in the migration folder called __init__.py
In my case I was pointing to a different databases between my local server and the production server. The database that the production server was pointing to was a few versions behind, so the server could not locate the relation. If your issue were localized to one environment, check the configs first.
Another method to fix relation does not exist error
Create same table in db(postgre, mysql) using sql query tool
now comment your model in models.py and admin.py
run migration using :
python manage.py makemigrations app_name
python manage.py migrate
now uncomment and run migrations command again
I encountered same issue and fixed using following method, I am using postgres(pgadmin 4).
How to run Django-simple-blog? I tried to install django-simple-blog but could not find the manage.py file to run it. Can I get a solution or another simple blog?
Django has a concept of apps and a concept of projects. A project will have a manage.py file like you mention, and will also have a settings.py file that declares all of the apps that the project uses.
django-simple-blog is an app, meaning you install it within an existing project. After this explaination, the rest of the steps found here should be easier to follow: https://github.com/drager/django-simple-blog/blob/master/README.rst
The remaining steps are to:
Add 'simpleblog' to INSTALLED_APPS in your settings.py file
run the command python manage.py migrate from your project root
include 'simpleblog.urls' into any of your urls.py file
I have 2 related questions.
I have deleted all my migrations, including the migrations directory, from all the apps in my project. I've started with a clean database. But still, when I run ./manage.py makemigrations, django says there are no changes to be made. How do I completely start over with migrations? No squashing, just starting over.
It seems that when I call makemigrations, django consults the database. I'd like my codebase to be the only source of truth for my migrations. Is there a way to do this?
If an app doesn't have a migrations/ directory with an __init__.py file (even on Python 3), Django won't create any migrations if you don't specify the app name. You either have to create the directories and __init__.py files, or you have to call ./manage.py makemigrations <app_name> for each app, which forces Django to create the directory.
It doesn't. It may connect to the database during the makemigrations command, but the migrations are purely based on your models.
So i clone my repo, sourced it, ran pip on its dependencies and synced my db only the sync fails with:
DatabaseError: no such table: django_site
I checked the path vars in settings.py and it's pointing to my website root, in fact it even creates the db file specified in settings.py db.sqlite3 in the root when I run sync. If the path is correct and django can create the db file, why can't it populate it? What am I missing here ?
EDIT: I have tried commenting out "django.contrib.sites" from my installed apps and running manage.py migrate and migrate sites, I still get the same error and an empty database file.
Give a full traceback and your INSTALLED_APPS, please.
I had the same issue when installing both django-cms and django-debug-toolbar. The problem was in toolbar, so I solved it by deactivating toolbar for suncdb.