operational error: no such table django Every Time I create models - python

I am using Django 2.0, Python
Every Time I am creating new Model when I do
python3 manage.py makemigrations
it shows no changes detected and on migrate no migrations to apply
Even I tried
python3 manage.py makemigrations home
shows
Migrations for 'home':
home/migrations/0001_initial.py
- Create model FAQ
- Create model Topic
and then on
python3 manage.py migrate home
It still shows
Operations to perform:
Apply all migrations: home
Running migrations:
No migrations to apply.
and when I try to open that table in admin.
it shows
OperationalError at /admin/home/faq/
no such table: home_faq
however it'd worked when I'd deleted migrations and database and re migrated everything.
But I can't delete everything Every Time.
my models.py
from django.db import models
# Create your models here.
class Topic(models.Model):
topic_title = models.CharField(max_length=200,unique=True)
topic_discription = models.TextField()
no_of_posts = models.IntegerField()
def __str__(self):
return self.topic_title
class FAQ(models.Model):
question = models.CharField(max_length=800,unique=True)
answer = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)
author = models.CharField(max_length=100)
author_info = models.TextField()
def __str__(self):
return self.question
I've registered it on admin.py
from django.contrib import admin
from .models import Topic,FAQ
# Register your models here.
admin.site.register(Topic)
admin.site.register(FAQ)
EDIT #1:
Okay now I cleared all migrations and db.sqlite3 and ran
# python3 manage.py makemigrations
No changes detected
# python3 manage.py makemigrations home
Migrations for 'home':
home/migrations/0001_initial.py
- Create model FAQ
- Create model Topic
# python3 manage.py migrate home
Operations to perform:
Apply all migrations: home
Running migrations:
Applying home.0001_initial... OK
It does work but I rebuild the database for that.
Because It's not in production yet so its okay but It may become big problem later.
thing to notice is on
python3 manage.py makemigrations
It shows
No changes detected
but created migrations when
python3 manage.py makemigrations home
and have friendly response on
python3 manage.py migrate home
So even if there is any way to force rebuild the model in db then could do.( python3 manage.py force migrate )
^ Could be possible solution. if there exist force
EDIT #2:
Installed Apps:
INSTALLED_APPS = [
'home.apps.HomeConfig',
'UserProfile.apps.UserprofileConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

Related

Error in Django migrations, no changes detected

I've correctly done the "initial setup" with python manage.py migrate command and now in my mongodb database I see these collections:
__schema__
auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
with object inside them so I'm pretty sure that I did it correctly and if I do it now it says:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
I think this is all normal, then i created this models.py file
models.py
from django.db import models
# Create your models here.
class Customer(models.Model):
name = models.CharField(max_length=200, null=True)
surname = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
Here is part of my settings.py file:
INSTALLED_APPS = [
'mysite',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Folder Structure:
mysite
mysite
__init__.py
settings.py
other files
polls
migrations
other files
__init__.py
When I try to do python manage.py makemigrations I get this "No changes detected". Adding my app name doesn't solve the problem. I have a migrations folder with init.py (with the __) in it. I don't understand why it worked for the initial setup and now it doesn't. If i put some syntax error in the models.py file the messages I get after running the commands are the same, so maybe models.py is being searched in another folder? Really don't know, anyway hope i wrote everything necessary, I will reply as fast as I can if you need more informations!
Here is a few things to check to make sure your migrations will go through.
Make sure you created the app using django-admin startapp mysite.
Make sure you've saved the models file after adding the model into the mysite/models.py.
Add the app name to the installed app in the settings.py, but make sure to add it after all apps.
In your case I see that you've added mysite in the installed app when in reality you should add the app name not the project name, which in your case the polls app.

I keep getting no changes detected when migrating app in Django

I am following this video tutorial to learn Django: https://www.youtube.com/watch?v=F5mRW0jo-U4&t=909s
I am at the "Your first app component" section where he creates an app and then migrates it. I have followed every one of his steps so far yet I keep getting the no changes detected error when I migrate the app
I have tried researching ways to fix this but it doesn't seem like there is any one right way to do it, it's more of a case by case basis. I've made sure my app is under the install_apps section in the settings. I've tried python manage.py makemigrations but that tells me there are no changes detected. I've tried "python manage.py makemigrations product" to be more specific but it tells me that App 'product' could not be found. Is it in INSTALLED_APPS?" even though it is in installed apps
currently this is my installed apps section:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'product',
after migrate and makemigrations command put your app name
like this
python manage.py migrate product
python manage.py makemigrations product
my case it's working
You didn't add your app name in 'Installed_Apps' in settings.py. Add this first and then run again.
for a proper migrations with 'python manage.py makemigrations' make sure that after creating your model class you save them before you typing any console code very important

Django 1.8, makemigrations not detecting newly added app

I have an existing django project with a,b,c apps. All of them are included in installed apps in settings file. They have their own models with for which the migrations have already been ran. Now, if I add a new app d, add a model to it, include it in installed apps and try to run a blanket makemigrations using python manage.py makemigrations I get a no changes detected message. Shouldn't the behaviour be like it detects a new app and run an initial migration for that? I know I can do it manually using python manage.py makemigrations d but I want to do it with using python manage.py makemigrations command. Can someone provide explanation for this behavior?
If you create a new app manually and add it to the INSTALLED_APPS setting without adding a migrations module inside it, the system will not pick up changes as this is not considered a migrations configured app.
The startapp command automatically adds the migrations module inside of your new app.
startapp structure
foo/
__init__.py
admin.py
models.py
migrations/
__init__.py
tests.py
views.py
There are a few steps necessary for Django to pick up the newly created app and the models created inside it.
Let's assume you created a new app called messaging with the command python manage.py startapp messaging or by manually adding the following directory structure:
- project_name
- messaging
- migrations
- __init__.py
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
- project_name
- __init__.py
- settings.py
- urls.py
- wsgi.py
- manage.py
In settings.py you need to add the app to INSTALLED_APPS like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'messaging.apps.MessagingConfig'
]
After adding models to models.py, e.g:
from django.contrib.auth.models import User
from django.db import models
from django.db.models import DO_NOTHING
class Thread(models.Model):
title = models.CharField(max_length=150)
text = models.CharField(max_length=2000)
class Message(models.Model):
text = models.CharField(max_length=2000, blank=True, null=False)
user = models.ForeignKey(User, blank=False, null=False, on_delete=DO_NOTHING)
parent = models.ForeignKey(Thread, blank=True, null=True, on_delete=DO_NOTHING)
Django will automatically make migrations when running the command
python manage.py makemigrations
or if you only want to create migrations for the messaging app, you can do
python manage.py makemigrations messaging.
The output should be:
Migrations for 'messaging':
messaging\migrations\0001_initial.py
- Create model Thread
- Create model Message
If Django still does not pick up your new app and models, please make sure that your python classes correctly inherits from models.Model as in Thread(models.Model):, and that you define some Django model fields inside the class, e.g. text = models.CharField(max_length=2000, blank=True, null=False)
After your migrations successfully have been created, you can applythem using
python manage.py migrate (applies all migrations)
or
python manage.py migrate messaging (applies messaging migrations only)

error: App 'polls' could not be found. Is it in INSTALLED_APPS? *But polls is in INSTALLED_APPS in settings.py. I

I am trying to create the 'polls' part of my Django project/website and the tutorial (https://docs.djangoproject.com/en/1.8/intro/tutorial01/) says that before we 'activate' the models we have to include 'polls' in the INSTALLED_APPS library in settings.py. I did that, then I do--> $ python manage.py make migrations polls. This gives me the error--> App 'polls' could not be found. is it in INSTALLED_APPS?. What do I do from here?! I synced my database after I added polls, still doesn't work.
172-16-22-166:mysite manuelgomez$ cd
172-16-22-166:~ manuelgomez$ cd mysite
172-16-22-166:mysite manuelgomez$ ls
chocolate llama nuts story
db.sqlite3 manage.py polls urls.py
fts mysite settings.py
172-16-22-166:mysite manuelgomez$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
No migrations to apply.
172-16-22-166:mysite manuelgomez$ python manage.py syncdb
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
No migrations to apply.
172-16-22-166:mysite manuelgomez$ python manage.py makemigrations polls
App 'polls' could not be found. Is it in INSTALLED_APPS?
172-16-22-166:mysite manuelgomez$
In settings.py you will find INSTALLED_APPS and you must add the polls as shown below.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls'
]
add setting like this
INSTALLED_APPS = (
...
'you_app_name',
)
a simple : python manage.py makemigrations do the trick for me and will detect your models and make migrations
Remove the app from comand. Example: python manage.py makemigrations
Did you check database?
You should find "your app" at 'app' column in "Django_migrations" table.
if not, you should type "python manage.py migrate" command, "DJANGO_MIGRATIONS" table is created.
(In case of using window and cmd, you should restart cmd.)
Good luck.

"python manage.py syncdb" not creating tables

I first ran
python manage.py syncdb
and it created the database and tables for me, then I tried to add more apps, and here's what I did:
create apps by
python manage.py startapp newapp
Then I added 'newapp' to INSTALLED_APPS in setting.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'newapp',
)
At last I ran syncdb:
python manage.py syncdb
and here's the result I get:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
I checked my db and there is no table named newapp, no table's name including newapp.
I also ran into this issue, and was able to get around it by removing the migrations folder inside my app. Somehow that had already gotten created and was tricking syncdb into thinking that it was fully migrated already, but those migration scripts didn't actually do anything useful. Obviously don't try this if you actually have migrations you want to save, but I was working with a brand new app and models.
If you run:
python manage.py inspectdb > somefile.txt
You can get quickly check out if your database structure is matching your django models.
I tried most of the ideas above:
making sure the models.py is imported (verified that the module executed during a makemigrate),
deleting the migrations folder
setting managed = True (this is default anyways),
used the python manage.py inspectdb (which correctly dumped the table, if I had created it manually).
The key was simply to run a makemigrations on the app separately:
python manage.py makemigrations <app_name>
as part of performing the makemigrations step. Then you do
python manage.py migrate
afterwards as usual.
(applies to Django 1.10, using Postgres 9.5).
Django documentation
Credit, related post
i got same problem, but i didn't have any "migration" folder. I solved it like below.
I just added app_label with the model code,
class MyModel(models.Model):
...
class Meta:
managed = True # add this
app_label = 'myapp' # & this
Also, make sure it is discoverable by referencing it in myapp/models/__init__.py
from model_file.py import MyModel
You should be using this command
python manage.py migrate
where you were runing syncdb from the location where manage.py resides
I was having the same problem and noticed that it had created a db.sqlite3 file that didn't seem empty:
$ ls -l
-rw-r--r-- 1 sauron staff 122880 Jul 31 01:22 db.sqlite3
I tried running sqlite again using the filename as an argument and that worked:
$ sqlite3 db.sqlite3
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE "auth_group" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(80) NOT NULL UNIQUE
);
... many rows ...

Categories

Resources