Missing DB table for django-background-tasks - python

I'd like to regularly run a backend task in my Django app and found django-background-tasks (https://pypi.python.org/pypi/django-background-tasks).
Following the page, I
pip installed the package
added it to INSTALLED_APPS
created a test method annotated with #background(schedule=5)
However, once I call the method, I get "django.db.utils.OperationalError: no such table: background_task"
Running python manage.py migrate only tells me "No migrations to apply" (the DB has tables for other registered Django apps).
Is the django-background-tasks package incomplete?
How do I create the DB objects for it?

As Moses and Ravi suggested, python manage.py makemigrations background_task needs to be ran first so that python manage.py migrate can create the table.

Related

Create Django app with no User model - 3.1.3 wants to migrate auth_user?

I've got a Django application which has been happily humming along now for quite some time (2 years or so).
It's on 3.0.10 currently - when I tried to upgrade to 3.1.3, it says there was a migration for the auth application. No worries! Never been an issue before...
I ran python manage.py migrate and got the following error:
"Cannot find the object "auth_user" because it does not exist or you do not have permissions."
Which, I suppose would be true because we do not have a User model at all in this application. There is no auth_user table, that is correct. In other apps we have an AbstractUser that routes to a table named: org_user - but again:
this particular app (and project) do not have any User model associated with them
Obviously this (apparently, now) leads to some issues.
Any thoughts on how to get around this? I thought about removing auth from installed apps and tried that but it led to more issues when trying to runserver.
You can fake a migration using --fake option:
python manage.py migrate --fake auth
python manage.py migrate
But I suspect the error you get could be symptomatic of a project design issue, such as not setting properly settings.AUTH_USER_MODEL
Solution 1 :- Run these commands :-
python manage.py migrate auth
python manage.py migrate
Solution 2 :- Try to copy your Project in another folder and then run migrations.

DJANGO HEROKU :django.db.migrations.exceptions.InconsistentMigrationHistory:

i have pushed my django application to heroku
and when i tried to fireup the url where my application is
i just got this error
ProgrammingError at /
relation "accounts_user" does not exist
LINE 1: ..."."is_active", "accounts_user"."date_joined" FROM "accounts_...
so i have figured out that the issue is with migration so i tried to run the makemigrations command in heroku
but unfortunately it returned as a failure with an error message
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration posts.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.
so what i have tried is i have completely removed the migrations and database and runnned makemigration and migrate command in my local server and then i pushed it back to heroku but still its of no use
my local is running perfectly fine
i'm not sure where exactly the migrations issue is , in my heroku django application
i want to remove the previous migrations and have a clean database
but i have no idea on how to do that
I was facing the same issue yesterday. I was using Django rest and sqlite3 DB.
To solve that error I have clear all migration and delete a migration folder and clear DB file. After that, I have run migrations specific for that module and then run migrate command then it works fine for me. I hope it will work for you as well.
e.g.
$ python manage.py makemigrations module_name
$ python manage.py migrate module_name

Database Api Python/django

I am working on a tutorial on Django
When I invoke the interactive shell to access the database API using the command python manage.py shell, I receive the following prompt
In [1]:from music.models import Album, Song
In [2]: Album.objects.all()
Then the next line gives me this
OperationalError: no such table: music_album
Like the error says, it means that your database has no tables that correspond to these models.
Typically that means that you forgot to migrate your database. By running:
python manage.py makemigrations
Django will construct files (in someapp/migrations/) these files describe the steps that have to be carried out such that tables are constructed with the correct columns, or later in the progress columns can be renamed, tables removed, new tables constructed. If you run the command, you will see new files popping up that describe these migrations.
But now you still did not migrate the database itself, you only constructed the files. It is for example possible that you want to add custom migrations yourself. In case the migration files are correct, you can run
python manage.py migrate
to change the database accordingly.
In order to run the migrations, the database has to be specified correctly in the settings.py file. But since you obtain an OperationalError that complains about the table not being found, I think this has already been handled (correctly).
See the Django topic on migrations [Django-doc] for more information.
Configure your database connection in your settings.py and, after creating Django models, you have to execute two commands:
python manage.py makemigrations
python manage.py migrate
Then tables will be created in the database and you will be able to use then in the shell

Python social auth in Django, makemigrations detects no changes

I was following the documentation to get python social auth on my django project
https://python-social-auth.readthedocs.org/en/latest/configuration/django.html
And after adding 'social.apps.django_app.default', to the INSTALLED_APPS in my settings.py I run this:
python manage.py makemigrations
I get this
No changes detected
Shouldn't this command be doing something. Because without this I can't migrate to create the tables that are needed for the auth.
EDIT:
I've also tried this command, and still ended up getting the same result
python manage.py makemigrations main
where 'main' is the name of my app
Today i ran into this problem. The error is in the documentation itself.
You should run $ python manage.py migrate directly. It creates tables in the database.
All the old tutorials used makemigrations, I think it was used in earlier versions of django.
My answer will be cover some basics so one can understand this types of errors easily.
Let me clear some basic terminology about migrations in the latest versions of Django(From 1.7 to under development ones).
In older versions of Django when One has to make changes in Models (Eventually in Database) then One has to use South app to apply changes in Database without affecting older database.
Django developer community has included this south app in and after Django 1.7 and also provided some simple commands to apply migrations.
When one install New app(Above question scenario) or one make changes in existing models and wish to apply changes in database then one has to tell database about what changes they want to make. To do so one has to make migrations and below is the command.
$ python manage.py makemigrations app_name
or If it is initial then no need to specify app_name, it will consider all apps.
This command will generate migrations files which will include instructions for database to make what tables and what are the attributes of that table and what will be the relationships between tables and what are changes in the current tables etc etc.
Now one has to run below command to apply this all changes in database.
$ python manage.py migrate app_name
or If it is initial then no need to specify app_name.
Please shoot any questions If you have any and please take a look at Django's official documentation on migrations for more information.
The possible reason is your project doesn't use any database models of 'social' application yet. Add a URL in your urls.py, link it to 'social' urls.
to Django < 1.8
INSTALLED_APPS ['social.apps.Django_appConfig',]

Django first app: error during the creation of sql tables from my models

I'm new in Python and I would to start some tutorial using Django.
I would use PyCharm as IDE.
So, I tried to create my start Django project.
I use a Mac, Python 2.7.5 version, Django 1.7.5 version.
I follow this tutorial to create my first Django app.
When I try to crate SQL Tables (in Pycharm I use this command: Alt + R, sql command and the name of my app example) I have this error:
bash -cl "/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py sql FirstExample /Users/Gianluigi/PycharmProjects/mycompany"
CommandError: App 'FirstExample' has migrations. Only the sqlmigrate and sqlflush commands can be used when an app has migrations.
Process finished with exit code 1
You don't need to use sql command. Assuming you have your database information correctly set up in settings.py, your python manage.py ... will be able to do everything for you.
In this case you would do python manage.py makemigrations mycompany followed by python manage.py migrate.
Lets say we have an app users. Add that app to settings
models.py:
class User(Django_User):
observations = CharField(max_length=2048, null=True, blank=True)
you should run the following command first to create the migrations:
python manage.py makemigrations
This will create your database tables
That's it!
https://docs.djangoproject.com/en/1.7/topics/migrations/
https://www.jetbrains.com/pycharm/quickstart/django_guide.html
while following this link, in CREATING DATABASE section when it says use the magic Ctrl+Alt+R shortcut twice:
we actually use it once and in console type makemigrations and then migrate

Categories

Resources