MySQL Test DB Failing - mysql

I set up a Django 2 app (and Python 3.6) with a remote MySQL DB (using mysqlclient), with proper permissions.
When I run my unit test, I get the following error: django.db.utils.ProgrammingError: (1146, “Table ‘test_<db>.<db>’ doesn’t exist”).
When I manually open the website at localhost, everything works fine.
Technically I could create a local dev DB for testing, but I would prefer that the test DB is created automatically.
EDIT
It seems like the issue was related to my tables not being managed by Django, see here.

I believe this is a duplicate.
Django : Table doesn't exist
If the table is missing, make sure you've got the right migration file (python manage.py makemigrations) and that you've applied it (python manage.py migrate).

What worked for me:
Changing my tables to managed = True.
Deleting migration files (except __init__.py).
python manage.py makemigrations
python manage.py migrate
And now my unit tests are running.

Related

Getting error after changed sqlite to mysql, table doesn't exist?

i've finished developing a django project and wanted to change sqlite3 to MySql for a better database option. I tried in an empty project to change database, it worked like a charm. But now i changed db of my project and when i try to do python manage.py makemigrations it returns;
django.db.utils.ProgrammingError: (1146, "Table 'tvekstra-django-tracker.tvchannels_channels' doesn't exist")
Any help is appreciated, thanks.
I guess you have code that is trying to query the database before makemigrations runs. Here is a similar question. To help with your specific problem, you need to show the full traceback which will show where the query is occuring, and your views.py.

AWS Django Elastic Beanstalk migration error

Background
I created a website using Django and have it deployed on AWS EB with a MySQL Amazon RDS database. Recently I added a new field to one of the existing models in my models.py file. Everything worked fine on my local server (uses local SQLite, not RDS), but when I deployed the new update to EB I get the following error when I visit any page:
OperationalError at /
(1054, "Unknown column 'old_model.new_field' in 'field list'")
Question
What did I do wrong to get the error above and what can I do to fix it?
db-migrate.config:
container_commands:
01_migrate:
command: "django-admin.py migrate"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: app.settings
Creating a manual migration with python manage.py makemigrations, if this doesn't found any new changes in models try the same with your app name and then migrate the DB with python manage.py migrate
This should solve your issue. if this doesn't solve your issue, clear all the migrations file the again run makemigrations
I suspect migrations are not running properly.
Create an EC2 instance and try to run migrations on it connected to RDS instead of local sqlite DB.
Make sure your EC2 instance and RDS are on the same VPC.
After that deploy your app to EB.

Django django_admin_log' doesn't exist

I have setup a new database with MySQL and run
python manage.py migrate
then run
python loaddata db.json
to load data from my previous sqlite database.
But now when I try to access the admin pages I get
ProgrammingError at /admin/
(1146, "Table 'myapp.django_admin_log' doesn't exist")
Other answers say to run syncdb but it not longer exists.
Any ideas?
Add django.contrib.admin in your INSTALLED_APPS, then run python manage.py migrate admin to create initial db tables for admin application. It will create django_admin_log table for you.

rake db:migrate runs in development AWS Beanstalk

I'm new to Beanstalk. I've created a Rails application and set the database production configuration to use the environment variables hopefully provided by AWS. I'm using Mysql (mysql2 gem), and want to use RDS and Passenger (I have no preference there).
On my development environment I can run the rails application with my local Mysql (it is just a basic application I've created for experimentation).
I have added the passenger gem to Gemfile and bundled, but I'm using WEBBrick in development still.
The only thing I did not do by the book is that I did not use 'eb' but rather tried from the console. My application/environment failed to run as while "rake db:migrate" it still thinks I wanted it to connect to the local Mysql (I guess from the logs that it is not aware of RACK_ENV and hence uses 'development').
Any tip? I can of course try next the 'eb', yet would prefer to work with the console.
Regards,
Oren
In Elastic Beanstalk (both the web console and the cli), you can pass environnement variables. If you pass the RAKE_ENV variable, you will change your environnement.
After that you still need to pass your database parameters (db password, name, ...) which should not be hardcoded into the code.
Have you tried to run
bin/rake db:migrate RAILS_ENV=development
?
I got the same issue and that worked for me.
I recommend you enter to EC2 instance through this command "eb ssh" (The first time you need specified you .pem file, if you have not one you can create in IAM services) and check your logs for more information about yours error.
If you have problems when you are uploading your code (eb deploy) you have the log in this file: "/var/log/eb-activity.log" (Remember this file is in your EC2 instance)
If you have a problems with your app, you can read the logs in this files: "/var/app/support/logs/production.log" or "/var/app/support/logs/passenger.log"
Other recommedations is install EB CLI version 3. for manage your eb instance
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html
I believed that Elastic Beanstalk will run 'rake db:migrate' by itself. Indeed it seems to try, but that is failing. I gave my bounty to 'Yahs Hef', even though I will only try this evening (UK). My disorientation with AWS caused me to forget this easy solution, of running the migration by myself. If this does not work by itself, I'll simplify the database configuration as possibile.

Non-NULL datetime values retrieved as NULL when Django server put behind Apache

I'm having a weird problem that's only reproducible in my prod environment. I have some Django code:
from report.models import Report
report = Report.objects.get(id=1)
completed_datetime = report.completed_datetime
On prod completed_datetime will be None even though the value in the database is not NULL. Normally when I have a bug like this, my approach is to reproduce the problem in the shell or in runserver where I can use pdb to observe the problem more closely. However, when I execute the identical code in the shell on the same database, I get the correct value.
Since the problem completely goes away when running the server with runserver I've had to take a different approach. One thing I have tried is adding the following line to the end:
report_dict = report.__dict__
which when inspected on prod has None for the attribute completed_datetime, but is fine in any other environment.
I have not forgotten to restart Apache. I have tried zero-ing the tables and re-migrating everything, but to no avail. I am running Django 1.8 with MySQL 5.6.19 and Apache 2.2.22. Any suggestions on how to debug this problem further?