Using Django ORM inside Tornado, "syncdb" doesn't work - python

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

Related

Django makemigrations does not create admin databases

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.

Django: sqlite3.OperationalError: no such table in python shell

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)

Error when running migrate command Django

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.

Django-constance: relation "constance_config" does not exist

I am having trouble using django-constance.
I've followed the steps here: https://django-constance.readthedocs.io/en/latest/index.html:
pip install "django-constance[database]"
add 'constance' and 'constance.backends.database', to INSTALLED_APPS
placed the following at the bottom of the settings file (it isn't callled setings.py but common.py):
CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend'
CONSTANCE_DATABASE_PREFIX = 'constance:my_app_name:'
CONSTANCE_CONFIG = {
'THE_ANSWER': (42,
'Answer to the Ultimate Question of Life, The Universe, and Everything'),
}
then ran python manage.py migrate database
But a table for dynamic settings wasn't created. This what happpens when I try to list constance settings:
$ python manage.py constance list
...
django.db.utils.ProgrammingError: relation "constance_config" does not exist
LINE 1: ...ce_config"."key", "constance_config"."value" FROM "constance...
I am running Python 3.5.2, Django 1.11.3 and django-constance 2.0.0.
Any clue what is going on?
I am not exactly sure why, but here's what worked:
the database initial migration was present
$ python manage.py showmigrations database
database
[X] 0001_initial
but the table itself was not
\dt *constance*
No matching relations found.
so I've removed that migration from django_migrations
delete from django_migrations where app = 'database';
re-ran the migration
python manage.py migrate database
and that's it. constance list behaves:
$ python manage.py constance list
THE_ANSWER 42
Check INSTALLED_APPS on the your project settings file, if you want using database backend change similar below:
INSTALLED_APPS = (
# other apps
'constance.backends.database',
)
Read more

migrate command in south cause this error:table "model_name" already exist

I have a project named HubHub that contains 2 apps named DrHub and AgencyHub,when changing models syncdb doesn't change them and I tried to use south :
in settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'grappelli',
'django.contrib.admin',
'south',
'AgencyHub',
'DrHub',
)
I ran the first command to config first migration based on this tutorial: http://south.aeracode.org/docs/tutorial/part1.html
python manage.py schemamigration DrHub --initial
and the second command:
python manage.py migrate DrHub
but this command cause this error:
table "model_name" already exist
"model_name" is the name of first model of models.py in DrHub
If you found any solution then post answer.
thanks in advance
It`s because initial migration will create all tables in database for you. And you have an existing database with existing tables. You can either wipe you database and then do a migrate or you need to use a --fake option in migrate. Docs here
python manage.py migrate DrHub --fake
Please remove the database table and try to create sync db.

Categories