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.
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'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)
I want to define a fixed username and password for superuser creation in Django's syncdb (after it has been executed). The method I'm using below was working in an older version of Django (I guess 1.6) but now it's not working.
I have this fixture file initial_data.json :
[
{
"fields": {
"username": "me",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": null,
"groups": [],
"user_permissions": [],
"password": "pbkdf2_sha256$...",
"email": "a#a.co",
"date_joined": "2015-04-23T01:13:43.233Z"
},
"model": "auth.user",
"pk": 1
}
]
when I add this in settings.INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
)
and run python manage.py syncdb, I get the following error :
django.db.utils.OperationalError: Problem installing fixture 'path/initial_data.json': Could not load auth.User(pk=1): no such table: auth_user
What should I do?
Can I change the order that fixtures load to ensure that auth_user table is created before this fixture is loaded?
Or any other way to automatically create a superuser in Django?
Thanks in advance.
I solved my problem, however it is definitely not the prettiest solution.
I saw that when I run heroku run python manage.py syncdb, I get the following
Operations to perform:
Synchronize unmigrated apps: myproject, permissions, myapp1, myapp2
Apply all migrations: auth, flatpages, admin, contenttypes, sessions, sites
Synchronizing apps without migrations:
Creating tables...
Creating table app1_model1
Creating table app1_model2
...
So I thought what if i migrate the included apps in reverse order :
heroku run python manage.py migrate sites; heroku run python manage.py migrate sessions; heroku run python manage.py migrate contenttypes; heroku run python manage.py migrate admin; heroku run python manage.py migrate flatpages; heroku run python manage.py migrate auth;
and it worked! I have no idea why, but it did. Maybe this will help someone else too.
I'm trying to deploy my Django application to Heroku. The migrations are in my local Git. When I try:
git push heroku master
heroku run python manage.py syncdb
It applies the migrations and also promts me to create superuser, which I successfully do. Now the application is up and running, however when I try to log into the Django admin it's throwing:
OperationalError no such table: user_user
When I try
heroku run python manage.py makemigrations
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
It applies all migrations, but fails to create superuser throwing:
django.db.utils.OperationalError: no such table: user_user
Either way I can not have my database set up and migrated on Heroku.
My database settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
My user model is:
class User(AbstractUser):
rating = models.PositiveIntegerField(default=settings.DEFAULT_USER_RATING)
Django version is 1.7.1.
How do I get my database tables created on Heroku?
You must not use sqlite3 on Heroku.
sqlite stores the database as a file on disk. But the filesystem in a Heroku dyno is not persistent, and is not shared between dynos. So, when you do heroku run python manage.py migrate, Heroku spins up a new dyno with a blank database, runs the migrations, then deletes the dyno and the database. The dyno that's running your site is unaffected, and never gets migrated.
You must use one of the Heroku database add-ons. There is a free tier for Postgres. You should use the dj-database-url library to set your database settings dynamically from the environment variables which Heroku sets.
Also, for the same reason, you must do manage.py makemigrations locally, commit the result to git, then push to Heroku.
You can use postgresql:
In settings.py add(at the end of file):
# ie if Heroku server
if 'DATABASE_URL' in os.environ:
import dj_database_url
DATABASES = {'default': dj_database_url.config()}
In requirements.txt add:
dj-database-url
psycopg2
Now you can run:
heroku run python manage.py migrate
pip install django-heroku
Add import django_heroku at top of file settings.py
Place django_heroku.settings(locals()) at the very bottom of settings.py
It will automatically configure your db. You can learn more on their website
What version of django you are using..?
If you are using django>=1.7 you need to run migrate
After adding models you need to do
python manage.py makemigrations then python manage.py migrate
If your project already contain migrations you can directly run python manage.py migrate command.
If you miss any step mentioned above please do that.
I'm using Django ORM inside Tornado, and everything is going well except that "syncdb" doesn't work.
So this is my directory structure:
APP_NAME/
APP_NAME/
models.py
settings.py
__init__.py
manage.py
database.db
When I do python manage.py syncdb, the message shows:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
And the database.db file is created (I'm using splite3), but no tables are created. It's worth noting that if I introduced some syntax error into models.py, syncdb would complain, so it seems like syncdb is able to find my models.py. It's just that it's not creating tables for some reason.
For your reference, here is my settings.py (the only thing I omitted is SECRET_KEY):
INSTALLED_APPS = (
'APP_NAME',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'database.db',
}
}
Thank you.
P.S. I'm not sure if this is relevant, but I'm using Python 3.3.1 + Django 1.5.1 + Tornado 3.0.1
if you did
django-admin startproject foobar
then your settings should look like :
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'foobar',
)
then
python manage.py syncdb
will give you
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table foobar_mymodel