I'm having trouble with my database at Heroku:
I've added some models for my App and whenever I run makemigrations it detects those changes. However when I run migrate it only says no migrations to apply. Please help!
I'VE FINALLY FIXED IT!!! This is what I did:
First I did a backup Download by heroku pg:backups:restore capture
then heroku pg:backups:restore download
it made a "latest.dump" file
then I used heroku pg:reset
it deleted the recent database of host
then I applied my fixes on my models and forms
new fields has to have "defaults=None" for models with Data you want to preserve (If you won't do this, the merge for this model won't take effect and will be cancelled)
then I run heroku run bash
run python manage.py makemigrations appname
then migrate
and then used "pg_restore --verbose --clean --no-acl --no-owner -h yourDBhost -U yourDBuser -d yourDBname latest.dump" (Your database credentials will be found on your postgresql addon settings) and then type or copy-paste yourDBpassword(from credentials also) to merge the backed up database with the new one with changes
and know it's done and perfectly working!
I have an django project on Heroku and I need to update the DB daily. Manually i would open manage.py shell and write there this:
from app import views
views.function()
One way i found to do that automatic is through a heroku scheduler, however I would like to know if it is possible to tell the shell what commands should it run.
I was doing this:
python -c "from app import views;views.function"
but it gives me an error because that should be done on the shell instead of the command line, so is it possible to tell the shell what should it write?
Thanks :D
You can write a custom django command, something like my_command.py and call it from the command line:
python manage.py my_command
https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/
In my project, I have a routers.py with different router classes. Now, I am making a new app. I have created my models.py. I have also registered the app in the INSTALLED_APPS in settings.py. Then, I ran validate. Everything is fine. When I syncd thoughb, Django does not install the tables. I tried using
python manage.py sqlall <app_name> | psql <database>
Then, I get an error message saying:
psql: FATAL: password authentication failed for user <user name>
I noticed that the role does not exist in postgres. So, I created the role with login privilege createdb and password. Then, I get a different error message:
psql: FATAL: password authentication failed for user <user name>
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
And, it does not provide the original exception.
Any help is much appreciated.
It looks like the Django application is unable to log onto the DB.
In django's settings.py make sure you have the proper DB credentials setup:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': get_env_variable("DJANGO_DB_HOST"),
'NAME': get_env_variable("DJANGO_DB_NAME"),
'USER': get_env_variable("DJANGO_DB_USER"),
'PASSWORD': get_env_variable("DJANGO_DB_PWD"),
'PORT': '',
},
...
}
As you can see my credentials are grabbed from environment variables. You can hardcode them in for test purposes.
Then in the DB (mine is postgresql) create the user/grant it the correct privileges, for example:
ssh root#dbhost
su - postgres
createdb dbname
psql
GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser
That should do.
I recommend the following steps as well, if they are missing from your setup:
In your app's admin.py register your models with Django's admin:
# Register your models here.
admin.site.register(Model1)
...
admin.site.register(ModelN)
Then, assuming you have created the project already, run:
python manage.py migrate
(it's the syncdb equivalent. Read the docs about migrations).
If that command does not ask for the admin superuser then create your administrative user (i.e. the user who can manipulate the models through django's admin interface) with:
python manage.py createsuperuser
Fire up Django
python manage.py runserver 0.0.0.0:8000
and see what happens whan you visit your site and admin at
localhost:8000/
localhost:8000/admin
Please pardon me if you know all those things already. That is what I normally do in my dev environment (I also use virtualenv, of course).
So I'm following this tutorial http://rosslaird.com/blog/building-a-project-with-mezzanine/ for building a project with Mezzanine. I am extremely new to all of this stuff (including Linux and the command line) and frankly do not really know what I am doing. I am at this part of the tutorial:
Run this command within the same directory as your local_settings.py and settings.py files: python manage.py createdb
The tutorial says that after I enter the "python manage" command I will be "asked to create a super-user, to provide details that user, and to answer a few more questions". When I entered the command none of those questions showed up. Why is this? Thank you very much in advance.
So you are trying to run this on the commandline right (terminal)?
sudo -u postgres createuser --superuser $USER
sudo -u postgres psql
postgres=# \password [enter your username]
Enter new password:
Enter it again:
\q
createdb $USER;
Change $USER with your designated name.
Sorry that the link you provided is 404'd (Most likely because this post is over 2 years old) .
But... I think I found it here (sort of)... There's a few bits and pieces missing which might have made it confusing. The "manage.py" should be in the parent directory that's prior to where your "settings.py | local_settings.py | urls.py" files reside. Just make sure you are in the appropriate directory when running the manage command. An easy ls or ls -la command will show you where your files are at within the directory. I myself am a novice Mezzanine user. I've been playing around with it for a year now and hope this info can serve as a quickstart guide for setting up Mezzanine on PostgreSQL while also resolving your issue.
So... Once the following conditions are met you should be able to create a Mezzanine project with a PostgreSQL database instance. But first, make sure you have Mezzanine set up and running without warnings or errors.
For Mezzanine Setup...
Preconditions:
You've installed Python, pip, etc...
Get virtualenv & virtualenvwrapper installed and configured.
pip install virtualenv
pip install virtualenvwrapper
Make sure to add your environment variables for your virtual environments. Just point the paths to where you want your Mezzanine projects to live as well as the environments. An easy way to do it is edit your bashrc in the home directory. I like to keep my virtualenvs separate from my working directory but adjust the paths to how you like. Just sudo vi ~/.bashrc then add the following...
## Virtualenvwrapper Settings
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/envs/mezzanine/projects/live/here
source /usr/local/bin/virtualenvwrapper.sh
Note: Do i to insert text and when done editing type ctrl + c to exit the prompt then :wq! to write (save) and quit. Then close your terminal and open a new one so the new changes take effect. If not, restart your computer.
Next, cd over to your project directory cd ~/envs/mezzanine/projects/live/here and create your virtualenv for your Mezzanine project (It'll activate once created).
mkvirtualenv environment_name
You can deactivate your environment by simply typing deactivate in terminal. To re-activate your environment, type workon environment_name
Now you can install Mezzanine...
pip install -U mezzanine
Then create your Mezzanine project and watch those folders get created in your project directory...
mezzanine-project project_name
Collect your templates & static files.
python manage.py collecttemplates
python manage.py collectstatic
Now create your db instance (by default this will be SQLite if you haven't changed anything in settings.py .
Make sure you have your ALLOWED_HOSTS configured and edit your settings.py if you haven't already.
vi ~/envs/mezzanine/projects/live/here/project_name/project_name/settings.py .
ALLOWED_HOSTS = [
'127.0.0.1:8000',
'localhost',
'www.mydomain.com' #if you want to set that too.
]
Note: Don't forget to save your changes ctrl+c & :wq! .
At this point you should be able to go back a directory and run your server python manage.py runserver and get a response from your localhost (loopback address) at port 8000 by opening a browser and typing in 127.0.0.1:8000. (make sure your environment has been activated first)
Now For Your PostgreSQL Database...
Check this out. It's a pretty good resource and even touches base on virtualenv. You can also replace the Django references with Mezzanine (almost). The most important part is the database setup portion...
https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04
Install Postgres and dependencies. (You might need to run as sudo with -H flag here)
sudo apt-get install libpq-dev python-dev postgresql postgresql-contrib
Install psysopg2 (Might need sudo -H as well)
sudo pip install psycopg2
Login as "postgres" user:
sudo -su postgres
Run the psql shell command: psql. You should see the 'postgres=#' text.
Now, create your database (Remember to end psql statements with a semicolon;) CREATE DATABASE mydb;
Create database user: CREATE USER mydbuser WITH PASSWORD 'mypassword';
Set your user roles:
ALTER ROLE mydbuser SET client_encoding TO 'utf8';
ALTER ROLE mydbuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE mydbuser SET timezone TO 'UTC';
8: Then give the database user access rights: GRANT ALL PRIVILEGES ON DATABASE mydb TO mydbuser;
Type ctrl + d to exit shell, then type exit to exit postgres user.
Now, go to your settings.py and local_settings.py in your Mezzanine project's working directory and modify your DATABASES settings with the credentials you previously created... cd ~/envs/mezzanine/projects/live/here/project_name/project_name/ and then vi settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'mydbuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Note: Don't forget local_settings.py
Now you can create your database via manage.py.
python manage.py createdb
The above command should prompt you to do the initial setup of your database along with your site info, as well as create a superuser. Just follow the prompts. To create additional superusers just do
`python manage.py createsuperuser' .
Now go back a directory to the project root cd .. and run your server python manage.py runserver . And now... you should have your new Mezzanine project running on PostgreSQL. Congratulations!! :)
The tutorial is just wrong. The writer has got confused with the Postgres createdb command used earlier, and the actual manage.py command, which is syncdb.
You would be better off using the actual Django tutorial.
I dropped some table related to an app. and again tried the syncdb command
python manage.py syncdb
It shows error like
django.db.utils.ProgrammingError: (1146, "Table 'someapp.feed' doesn't exist")
models.py
class feed(models.Model):
user = models.ForeignKey(User,null=True,blank=True)
feed_text = models.CharField(max_length=2000)
date = models.CharField(max_length=30)
upvote = models.IntegerField(default=0)
downvote = models.IntegerField(default=0)
def __str__(self):
return feed.content
What I can do to get the tables for that app ?
drop tables (you already did),
comment-out the model in model.py,
and ..
if django version >= 1.7:
python manage.py makemigrations
python manage.py migrate --fake
else
python manage.py schemamigration someapp --auto
python manage.py migrate someapp --fake
comment-in your model in models.py
go to step 3. BUT this time without --fake
For those that may still be having trouble (like me), try this out:
Comment out all the URL's in the main app's urls.py
Then go ahead and run migrations:
$ ./manage.py makemigrations
$ ./manage.py migrate
The problem was alleviated by removing the ()'s
solved_time = models.DateTimeField('solved time', default=timezone.now())
to
solved_time = models.DateTimeField('solved time', default=timezone.now)
I got this answer from reddit
What solved my problem in situation when manage.py setmigration and then migrate to the SQL database is not working properly did is following:
Run command : python manage.py migrate --fake MyApp zero
After that: python manage.py migrate MyApp
And the problem that rises with connections.py and and after running correctly the migration command is solved! At least for me.
I'm using Python (3.x), MySQL DB, Django (3.x), and I was in situation when I needed after some time of successfully creating tables in my database, that some error regarding connections.py raises. But, above commands helped. I hope they will help all those who are having these type of problems.
I just ran migrations with the name of the app attached, for all the apps I had provisioned and that worked.
e.g. python3 manage.py makemigrations my_custom_app
After running for all of them I ran a migrate command to seal the deal.
python3 manage.py migrate. That was it. I'm still wondering why django behaves this way sometimes though.
none of the above solutions worked for me, I finally solved by
sudo systemctl stop mysql.service
sudo apt-get purge mysql-server
sudo apt-get install mysql-server
sudo systemctl stop mysql.service
In my case the code that I pulled had managed = False and I wanted the tables to be maintained by Django.
But when I did makemigrations the custom tables were not being detected or I was getting the error that the app_name.Table_name does not exist
I tried the following:
delete all the migration files inside the migrations folder (except init.py file) and then makemigrations then finally migrate
above 2 answers
this
PS: This solution is only feasible if backup is present or data is not important or you are just started creating the tables, as purging mysql will lead to loss of data
This is linked to the migration data in the scripts inside the project not matching with the migration scripts in the database as far as I could tell.
I solved this by the following steps :
Delete all the migration scripts under migration folder except __ini__
Make sure that the model.py contains the same structure as the table in the database and managed=True
Remove all Django Created tables like auth_user,... etc
Run the following code
$ ./manage.py makemigrations
$ ./manage.py migrate
This will create the migration scripts again and will apply it to your database.
I had this issue where I was playing with same database structure in production vs development. While dropping and recreating tables will probably resolve the issue, its worth checking your database itself and see if the model is actually correct. For myself I created the development database incorrectly with the table names all in lowercase while in production the first letter of tables were capitalized. I used the python manage.py inspectdb command on production db, and compared it to the model and realized that in the model it was trying to insert data to table 'test' instead of 'Test' for example. Hope that helps some of you in future.
I had a similar issue.
I had another python (with a class) file which need access to DB.
For some reasons, when running 'makemigrations' this file was processed (I guess this is linked to some import chains).
In this class, I had a method containing a default arg method(defaultModel=Model.get_default()) in the signature which was accessing to the default object in the DB (static method included in the Model class).
A the import time, this default arg was evaluated and as the table is not populated yet, it gives this error.
So I just set None for the default args and asks for the default model object inside the method. This solved the issue.
I have to face same issue and there are a couple of approaches, but the one I think is the most probable one.
Maybe you are loading views or queries to database but you haven´t granted enough time for Django to migrate the models to DB. That's why the "table doesn't exist".
Make sure you use this sort of initialization in you view's code:
Class RegisterForm(forms.Form):
def __init__(self, *args, **kwargs):
super(RegisterForm, self).__init__(*args, **kwargs)
A second approach is you clean previous migrations, delete the database and start over the migration process.
I tried all of the above tricks and never worked for me.
I commented on all imports and URLs that called a particular Table
In this solution, your data will be removed. I removed the app and created the app again. I copied the app folder somewhere and delete the app folder from my project. I commented on all lines in urls.py and files similar views.py and admin.py that use this app. also app name in settings.py.
In mysql:
truncate django_migrations;
truncate django_admin_log;
Do this for all models in your app and change n.
n is app id.
delete from auth_permission where content_type_id=n
delete from django_content_type where app_label='appname'
python manage.py startapp your_app_name
Then uncomment previous lines and restore files and run
python manage.py makemigrations
python manage.pt migrate
I faced the same problem, some of the above mentioned answers seemed not to work for me, but here's a simple 4 step solution:
1) Delete the migrations files below __init__.py (don't delete __init__.py) in your specific app.
2) python manage.py makemigrations AppName
3) python manage.py migrate --fake AppName zero
4) python manage.py migrate AppName
Hope these works for you.
I faced the same issue earlier when I accidentally deleted my migrations folder in an app. I was able to fix it by running manual makemigrations for that specific app.
Here's the fix for Windows,
py manage.py makemigrations <your_app_name>
py manage.py migrate
For other OS you need to replace py with python3 or python
I hope this helped fix your issue!
if the python manage.py migrate still doesn't work I mean when you do this it nothing do anything you can delete the app's migrations from django_migrations table after then do
python manage.py migrate