I'm following a django blog tutorial. When I used python manage.py shell and tried following commands in shell
from mainsite.models import Category, Tag
c = Category(name='category test')
c.save()
this error occured:
sqlite3.OperationalError: no such table: mainsite_category
This is my model.py in mainsite folder, which I think is correct:
from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
I have tried manage.py makemigrations, manage.py migrate and can successfully use manage.py runserver to see the website. So what is wrong here?
Edit: this is my INSTALLED APP in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
]
And my project structure
|____djangoBlog
| |______init__.py
| |____settings.py
| |____urls.py
| |____wsgi.py
|____mainsite
| |______init__.py
| |____admin.py
| |____apps.py
| |____models.py
| |____tests.py
| |____urls.py
| |____views.py
|____manage.py
|____README.md
|____requirements.txt
|____templates
| |____blog
| | |____index.html
Edit: When I run python manage.py makemigrations and python manage.py migrate, the output is as follows:
No changes detected
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
UPD:
Since your project structure is lack of migrations folder in mainsite app, it means that you haven't created migrations for that app. Run python manage.py makemigrations mainsite and then python manage.py migrate.
From docs:
To add migrations to an app that doesn’t have a migrations directory, run makemigrations with the app’s app_label.
And yes, you can create model with simple instance creation. From docs Creating objects.
Original answer:
You need to be sure that you've included your 'mainsite' app in INSTALLED_APPS in settings.py and then run migrations. After that you should be able to do the following:
c = Category.objects.create(name='category test')
c.save()
#Owen,
According to vishes_shell's answer, you'll have to do :
python manage.py makemigrations mainsite
the first time in order to generate a folder migrations that will contain your migrations, then you can just do
python manage.py makemigrations
In the futur if you add a new app, don't forget to make again the first command (changing the "mainsite" by the name of your app) before using the second one.
It seems like in your models.py you need an indentation at name:
class Category(models.Model):
name = models.CharField(max_length=100)
Related
I have a django project, this is the installed apps entry in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crawler',
]
when I run python manage.py migrate everything seems to be fine. But when I try to login into django admin page, It says
(1146, "Table 'stock_db.django_session' doesn't exist")
It seems to me that migrate is not creating django admin databases, but why?
I even tried deleting the database, creating it again and running python manage.py makemigrations and python manage.py migrate, It didn't create django_session table.
Migrating your models won't create Admin resources. Those need to be created separately in your admin.py. See more details here
https://docs.djangoproject.com/en/4.1/ref/contrib/admin/
Essentially you need to create a Resource class, specify the fields, define whatever you want:
class MyModelResource(resources.ModelResource):
class Meta:
model = MyModel
fields = [
"your_fields",
]
Finally, you need to register your resource:
admin.site.register(MyModelResource)
Try makemigrations first if it doesnt help continue reading
This type of problem can appear in many situations, most common one is that your migration file system has broken. Reason may be that you tried to edit migration files, or you deleted some of them, or if you work on big teams there may be conflicting migrations. Fixing migrations system takes a lot of time and effort, plus you should know how they work. If project is not in production and you just playing around, I recommend to clear db and delete migration files and run makemigrations and migrate again.
Also be aware if you have multiple apps with models referencing each other with foreign keys or whatever.
Always make sure you run makemigrations before migrate
First run your migrations using below commands:
python manage.py makemigrations appname
python manage.py migrate
If this doesn't work then delete your current migrations using Python manage.py squashmigrations and then re-run migration commands.
I am trying to deploy my first web project on a server. I am getting this error when running migrate and makemigrations:
ProgrammingError (1146, "Table '<user>$<dbname>.<table_name>'' doesn't exist")
Everywhere it says that you should run manage.py syncdb but this is obsolete , also when running manage.py --run-syncdb the same error is given.
models
class Book(models.Model):
name = models.CharField(max_length=350)
author = models.CharField(max_length=350)
category = models.CharField(max_length=200)
def __str__(self):
return self.name
snipppets from settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
} #Everything is replaced with the correct credentials
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main_app',
'accounts',
]
The DB is empty on the server. On my PC (when I developed it locally) it worked, it created the table for me when it didn't exist in the DB.
I am using Django 1.11 and Mysql 5.6.27.
I tried other answers but did not help me.
Can you please suggests what can I do to solve this issue?
You should use migrations mechanism: https://docs.djangoproject.com/en/1.11/topics/migrations/
./manage.py makemigrations # this creates migrations files
./manage.py migrate # this applies migrations
You don't need to use syncdb.
for the first time you creating your Django project in order to creating pre-installed apps (such as admin interface) database in django you need to use :
python manage.py migrate
it creates all necessary database tables and relations.
but every time you make changes to your app like creating new columns in your models, you can use:
python manage.py makemigrations [your app name]
the result will be something like below :
(env35) someone#shell:~/bookstore$ python manage.py makemigrations books
Migrations for 'books':
0005_publisher_family.py:
- Add field family to publisher
and finally you run:
python manage.py migrate
to Synchronizes the database state with the current set of models and
migrations.
I'm using django 1.8 and I'm having problems adding to my models.py. Currently it's:
from django.db import models
# Create your models here.
class Company(models.Model):
role = models.CharField(max_length=32, blank=True)
name = models.CharField(max_length=70, blank=True)
and it works perfectly fine but whenever I try to add to this and then run the server I get
OperationalError: no such column [added element]
For example I added founder = models.CharField(max_length=200, blank=True) and I ran the program and I got
django.db.utils.OperationalError: no such column: companies_company.founder
Run in your console this commands:
manage.py makemigrations app_name
manage.py migrate app_name
Every time when you change model in your app you should migrate changes to your db using makemigration and migrate commands. When you adding a new column to your db table you must add value of this column to all existing rows. You can do it by seting default value in your new field in your model.
Or set values when run migrate command ( django automatically propose this)
You can read about this in docs
This type of problem occurs when there are some operations on the model field in some other files such as forms.py or views.py other than models.py when you run makemigrations. If you read the traceback carefully you can figure it out from which file the problem is originating.
For example, if the traceback tells you some complaints in forms.py which may happen to use some of the model fields, just comment out the code that is working on the model fields and rerun makemigrations again. Hopefully it resolves the issue.
If it does, you can then remove the comments that you added before.
You have to re sync the database using python manage.py makemigrations
Maybe not optimal, but it worked for me doing that (and the default thing did not work):
delete all files apart from the init.py in migrations folder
delete the db.sqlite file
doing: python manage.py runserver (without this one previous to migrate, migrations, that did not work, I think it's linked to the views / templates calling tags, so has to run first)
python manage.py migrate
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
python manage.py createsuperuser
I found that clearing all data with python3 manage.py sqlflush and then python3 manage.py flush
Then makemigrations, sqlmigrate, and then migrate
It will delete all data in the SQL database including users and objects
$python manage.py syncdb
Operations to perform:
Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
the error printscreen
i don't know what the problem !
As the command line output says the problem is that you changed the models. The solution is to run python manage.py makemigrations and then python manage.py migrate.
The Best Thing You can do is, Delete the existing database.
In my case, I were using phpMyAdmin SQL database, so I manually delete the created database overthere.
Again run the following Commands:
python manage.py makemigrations
python manage.py migrate
python manage.py makemigrations <app_name>
python manage.py migrate
IT COULD BE BECAUSE YOU HAVE NOT YET REGISTERED YOUR APP IN SETTINGS.PY inside this file install your app (put the name of your app in quotes) after you can make python3 manage.py makemigrations and then python3 manage.py migrate (it can be python3 or python without the 3 ):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'new_app_name',
]
Hi my friend after all these steps if there was still this problem.
1- Clear all tables in the database.
2-Clear all history in the migrations folder of your project directory.
3-python manage.py makemigrations
4-python manage.py migrate
I have a Django project on a Centos VPS.
I created some models and debugged them so they validate and give no errors.
I have them in a "models" folder in my myapp and have added each model to the init file in this directory, for example:
from category import Category
I added the app to settings.py INSTALLED_APPS and ran:
Python manage.py syncdb
It appeared to work fine and added all tables apart from the ones for my app.
I then installed South and added that to INSTALLED_APPS and, tried syncdb again and ran:
Python manage.py schemamigration myapp --initial
It generated the file correctly but nothing was in it (none of the tables my models).
An example file in "models" folder (usertype.py)
from django.db import models
class UserType(models.Model):
usertype_id = models.PositiveIntegerField(primary_key=True)
description = models.CharField(max_length=100)
is_admin = models.BooleanField()
is_moderator = models.BooleanField()
class Meta:
app_label = 'myapp'
Any ideas what could be going wrong here and why I can't get anything to detect my models?
Run the following commands
python manage.py makemigrations yourappname
python manage.py migrate
Note it works on django 1.7 version for me.
you're misunderstanding the process of working with south. South isn't just another application, it's a managing tool. Your app needs to be a South application from the begining or converted to one. That being said, the process is like so:
Add South to INSTALLED_APPS
run syncdb for the first time
Add your app to INSTALLED_APPS*
run the south initialization command:
python manage.py schemamigration myapp --initial
migrate:
python manage.py migrate
If you want to convert a project:
Run syncdb after adding south
run:
manage.py convert_to_south myapp
And use south from now on to manage your migrations.
*p.s. - you can add both south and your own app at the same time, if you keep in mind to put south before your own apps. That's because django reads INSTALLED_APPS in order - it runs syncdb on all apps, but after installing south it won't install the rest and instead tell you to use the south commands to handle those
edit
I misled you. Since you put so much emphasis on the south thing I didn't realize the problem was you were trying to use models as a directory module instead of a normal file. This is a recognized problem in django, and the workaround is actually exactly as you though in the first place:
say this is your structure:
project/
myapp/
models/
__init__.py
bar.py
you need bar.py to look like this:
from django.db import models
class Foo(models.Model):
# fields...
class Meta:
app_label = 'myapp' #you need this!
and __init__.py needs to look like this:
from bar import Foo
Make sure it looks like this and it will work.
UPDATE 18/08/2014
The ticket has changed to wontfix, because apparently the bigger issue with the app_label has been fixed. Huzza!