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.
Related
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 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
I have well working project on local. I use postresql.
Ok. I create another database in postgres locally and specify new name/user/password in settings.py of project.
When I do
$ python manage.py makemigrations
I get error as
relation 'report_person' not exist
And I have tried delete (and not delete) directory 'migrations'. delete pycache. I tried specify Sqlite3 as database - but the same error.
Why I ask?
My project don't want to work with another database)
I succesfully push project on heroku. Specify parameters in settings.py. Create postgresql. But I can't make migrate on heroku) the same error.
I do makemigrations and git commit before push on heroku, but no result
Help me please.
Thank you
Have a nice day!
try migrating with --fake
python manage.py migrate --fake
python manage.py migrate yourappname --fake
and then again
python manage.py makemigrations
python manage.py migrate
Oh, I can't explain this situation. All problems in one little model.
It is from models.py:
from django import models
class Person(models.Model):
name = models.CharField(max_length=255)
position = models.CharField(max_length=255)
This part of forms.py (crazy):
from django import form
q = models.Person.objects.all()
qty = range(len(q))
PersonForm = type('PersonForm',
(forms.Form,),
{'person'+'_'+str(q[i].id): forms.BooleanField(label=q[i].name,
required=False) for i in qty})
And I must before migrations delete this 'PersonForm'. Then migrate will be succesfully in new any database. And after migrations I put this form again.
Ok. I don't undestand, why?)
I'm upgrading a django project from 1.6.11 to 1.7.9. I use DRF 2.4.4. Once everything is working fine, I'll upgrade to DRF 3.x
I started using django-oauth2-provider, but it can not work with django 1.7, so I'm moving to django-oauth-toolkit.
for the record, it does not work because HttpResponse does not accept mimetype anymore. it has to be 'content_type'. PRs for django-oauth2-provider are abandoned.
I pip installed it and added the oauth app to INSTALLED_APPS. Their docs suggest using syncdb and then migrate, although Django 1.7 deprecated syncdb. I tried to migrate it with ./manage migrate oauth2_provider unsuccessfully. It keeps reporting:
ValueError: Dependency on unknown app: provider
provider happens to be the old module for oauth, that was coupled with 2 of my apps.
I removed it from INSTALLED_APPS, commented all imports and usages of its classes, changed the model that had a FK to provider.oauth2.models.Client (I removed that field), and tried again. I got the same error.
The initial migration of one of the apps seems to be the only valid code that uses provider :
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('contenttypes', '0001_initial'),
('provider', '__first__'),
]
however, after this migration I removed that field. With South I'd probably do ./manage.py schemamigration myapp --auto and then migrate.
However, when I try to makemigrations myapp I get the same error.
I'm totally lost. How can I get rid off the old oauth module and get the new one? Do I have to run makemigrations for oauth2_provider or just migrate? My guess is that only migrate. Why can't I make a migration with the changes in myapp model?
This has happened to me. Try manually removing the "provider" app line in initial migration that refers to it. Alternately, you can delete all migrations and run "makemigrations" from scratch (remember, for initial migrations you have to do it once for every individual app).
I'm trying to get South to work - it worked fine on my PC, but I'm struggling to deploy it on my webhost.
Right now it seems that any changes I make to add/remove items from INSTALLED_APPS aren't being picked up by syncdb or diffsettings. I've added south to my list of INSTALLED_APPS, but the tables it needs aren't being created when I run syncdb. If I change other settings, they are picked up, it just seems to be INSTALLED_APPS that doesn't work.
If I run
from south.db import db
from the shell I get with manage.py shell, I don't get any import errors, so I don't think it's a problem with where south is. I've tried removing all my other applications (other than the Django standard ones), and tables for them still get created when I run syncdb.
Even if I delete INSTALLED_APPS completely, I still get the old list of INSTALLED_APPS when I run manage.py diffsettings.
Any ideas what I've done wrong?
Thanks,
Dom
If you write a migration for an application, syncdb wont work.
You have to use
manage.py migrate
syncdb wont work for applications which are hooked under migration using south. Those applications model change will be noticed only depending on south migration history.
South Migration Docs
The answer, it turns out, is that I'm a moron. I'd done this:
In settings.py:
...
INSTALLED_APPS = (
...
)
...
from localsettings import *
In localsettings.py
...
INSTALLED_APPS = (
...
)
...
I'd created localsettings.py from settings.py, to contain things only relevant to the current location of the project (like database settings), and forgot to delete the INSTALLED_APPS section.
Apologies for doing such a flagrantly stupid thing.