I have very strange situation here.
Problem:
I describe original problem inside this post, but to sum up:
After using the project for a while, makemigrations stop working for me.
(Yes I have set everything ok, setting are ok,.. please see my comments on previous post)
I decided that I can't figure this out, so I would start fresh. (thats why git is for :D )
So, I delete my django project, I create new virtual environment, and I also create new database and new user in postgres.
I update new database parameter inside my config file and try to run initial makemigrations but with no luch. It is still say that no new migrations are available.
I am desperate because I can't work on the project, so any solution would do :D
(virtual_environment) MacBook-Pro-2:MY_PROJECT marko$ python manage.py makemigrations connectors environment=local
I will use local settings.
No changes detected in app 'connectors'
All my migrations
(virtual_environment) MacBook-Pro-2:MY_PROJECT marko$ python manage.py showmigrations environment=local
I will use local settings.
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
connectors
(no migrations)
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
When lunch migration
(virtual_environment) MacBook-Pro-2:MY_PROJECT marko$ python manage.py migrate --fake-initial environment=local
I will use local settings.
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
The funny thing is that if I close postgres database, I still get the same text in terminal. I would guess that I should get some connection error.
database config yaml
database:
port: 5432
host: 'localhost'
user: 'user1'
password: 'user_password_123!'
db: 'db1'
and my settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': CONFIG['database']['db'],
'USER': CONFIG['database']['user'],
'PASSWORD': CONFIG['database']['password'],
'HOST': CONFIG['database']['host'],
'PORT': CONFIG['database']['port'],
}
}
After a lot of hours, I found solution, and it was actually my mistake.
I will post the answer just for those who make similar mistake :D
In fact, #Alasdair and #AdamBarnes was right. I wasn't connecting to new database, but not because the database settings was wrong (host, port, username, db,...)
What I have in settings.py was settings for database and database for unit tests.
# Databases start
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': CONFIG['database']['db'],
'USER': CONFIG['database']['user'],
'PASSWORD': CONFIG['database']['password'],
'HOST': CONFIG['database']['host'],
'PORT': CONFIG['database']['port'],
}
}
if 'test' or 'jenkins' in sys.argv:
DATABASES['default'] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test_database',
}
The problem was if statement below. For some reason he understand that this was always true. So program migrate sqlite internal test database.
I need to fix this with
if ('test' in sys.argv) or ('jenkins' in sys.argv):
This fix first problem, that when make migrations it leave empty database.
After this fix, migrate was populate database with default Django tables, but not with my tables.
The fix for this was to import my models.py inside admin.py file.
import connectors.models
Thank's a lot, everybody that give me great hints, to find the solution.
Related
I have a Django app that adds and displays stuff to my postgresql database online at elephantsql.com. My project file setup looks like this:
website/
website/
music/
playlist/
__pycache__/
migrations/
static/
templates/
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
.coverage
db.sqlite3
manage.py
My project works right now where when I run the server and go to /playlist/ it displays stuff correctly and connects to my postgresql database fine.
My settings.py DATABASES object looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'kxvmghva',
'USER': 'kxvmghva',
'PASSWORD': '--an actual password---',
'HOST': 'raja.db.elephantsql.com',
'PORT': '5432',
}
}
Now I'm trying to write test cases in my playlist/tests.py file, but when I try to run these tests I'm getting errors.
my testing file I'm trying to run /playlist/tests.py
from django.test import TestCase
from .models import plays
from django.utils import timezone
class AnimalTestCase(TestCase):
def setUp(self):
print("setup")
#Animal.objects.create(name="lion", sound="roar")
#Animal.objects.create(name="cat", sound="meow")
def test_animals_can_speak(self):
"""Animals that can speak are correctly identified"""
print("test")
#lion = Animal.objects.get(name="lion")
#cat = Animal.objects.get(name="cat")
#self.assertEqual(lion.speak(), 'The lion says "roar"')
#self.assertEqual(cat.speak(), 'The cat says "meow"')
When I run the command "python manage.py test playlist" I get these errors:
C:\Users\marti\Documents\kexp\website>python manage.py test playlist
Creating test database for alias 'default'...
C:\Python36-32\lib\site-packages\django\db\backends\postgresql\base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
RuntimeWarning
Got an error creating the test database: permission denied to create database
Type 'yes' if you would like to try deleting the test database 'test_kxvmghva', or 'no' to cancel:
if I type 'yes' it leads to this error:
Destroying old test database for alias 'default'...
Got an error recreating the test database: database "test_kxvmghva" does not exist
I've been trying to solve this error by searching it online and have tried stuff like giving my user 'kxvmghva' CREATEDB permissions as well as running this line in my elephantsql db:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO kxvmghva;
But I'm still getting these errors when trying to run the tests.py file for my playlist/ app. This is my first time setting up test cases for a postgresql database in django and I would really appreciate any help or guidance. Thanks.
I suppose the plan you are using for database is free/shared, in case which you do not have rights to create additional databases.
This is not a Django problem but restrictions of the service you are using.
As pointed in the first answer, ElephantSQL free tier (I suppose you're using this one) is shared. In that server, they have several DBs, so you have access to one and can't create another one.
I've tried to create an instance just for tests, but it also fails because "On PostgreSQL, USER will also need read access to the built-in postgres database." (from django docs).
So, my solution, and understand it is not the best of practices (because you'll have a different environment for testing) is replacing the engine for testing:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
},
}
This configuration works because (from the django docs):
When using SQLite, the tests will use an in-memory database by default (i.e., the database will be created in memory, bypassing the filesystem entirely!)
Two notes:
Use GitHub actions or a similar solution to test against PostgreSQL in each commit (this way, you reduce the danger of using different test and prod databases)
The easiest way to have a different setting config is to use separate files. I follow the logic in cookiecutter-django
I have been struggling with this aggravation for a little bit, and I have not been able to find a definitive answer elsewhere online.
I have a Django app that uses multiple databases; a default db and then a client db. The default db holds universal tables. The client db holds custom data/tables created by the client who uses my application.
I understand how migrations work, but I am using 2 databases that should not have the same models included when I run migrations. client's should have the client tables and the default db should hold the universal data.
It is also important to note (because of the question below) that I do not make application specific models (other than the default ones auto-generated by Django itself), I use 2 packages/applications for this: objects and objects_client, objects holds the default db models and objects_client holds the client db models.
client_db is also the name I use in settings.py
Now here is my issue:
I know I can run python3 manage.py migrate objects_client --database=client_db and python3 manage.py migrate objects --database=default, but I don't want to have to individually specify the admin, auth, contenttypes, and sessions migrations to the default database so I also run, python3 manage.py migrate, which obviously places objects_client in the default DB as well (which I don't want).
How do I go about runing the a version of the default python3 manage.py migrate command with an exclusion of an app, i.e. objects_client, without changing each model in that app to managed=false (because that is a pain)?
Here is the output of showmigrations if it helps decribe what I mean in more detail, these are the apps being migrated.
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
objects
[X] 0001_initial
objects_client
[X] 0001_initial
sessions
[X] 0001_initial
Set up a database routing scheme with Database routers and provide an allow_migrate method to determine if the migration operation is allowed to run on the database with alias db.
class Router:
...
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the objects_client app only appears in the 'client_db'
database.
"""
if app_label == 'objects_client':
return db == 'client_db'
return None
Finally, in the settings.py file, add the following
DATABASE_ROUTERS = ['path.to.Router']
pretty new to Django, i'm encountering an issue with a new model (and a new app 'blog' i made). The table blog_post didn't exist after configuring the model and makemigration.
Here is the all process i did. I'm following official tutorial:
Here is my blog/models.py:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=80)
text = models.TextField()
author = models.ForeignKey('auth.User', on_delete= models.CASCADE)
created_date = models.DateTimeField()
pub_date = models.DateTimeField()
def publish():
self.pub_date = timezone.now()
self.save()
mysite/settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig',
]
After the first
python manage.py makemigrations blog
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Post
python manage.py sqlmigrate blog 0001
BEGIN;
--
-- Create model Post
--
CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(80) NOT NULL, "text" text NOT NULL, "created_date" datetime NOT NULL, "pub_date" datetime NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "blog_post_author_id_dd7a8485" ON "blog_post" ("author_id");
COMMIT;
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
No migrations to apply.
So, here it is. The new table didn't seem to be created. I check with a SqLite utility and the is no such table: blog_post
I also check with django shell.
I double (triple) check the process:
Change your models (in models.py).
Run python manage.py makemigrations to create migrations for those changes
Run python manage.py migrate to apply those changes to the database
But i'm stuck at this point. Can someone tell me what i missed ? Thank you !
Here is my database settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
python manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
blog
[X] 0001_initial
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
I check if table exist with DB Browser for SQLite, but blog_post don't exist.
Link to the github repo: https://github.com/mothinx/juliengracia
The output of showmigrations shows that Django thinks the initial blog migration ran.
blog
[X] 0001_initial
The sqlmigrate output shows that the migration should have created the table. Therefore it looks as if the django_migrations folder and the database are out of sync.
You could try re-running the initial migration by faking back to the zero migration.
python manage.py migrate --fake blog zero
python manage.py migrate blog
If that doesn't work, and you don't have any important data in the database, you could delete the db.sqlite3 file and run python manage.py migrate again.
python manage.py migrate --fake APPNAME zero
And then you can run the migrate script
python manage.py migrate
Hope this helps!
I just use the following:
python manage.py makemigrations
python manage.py migrate
I've searched every Stack Overflow question on this error but none of the responses helped. I'm getting this error when trying to access the admin page of this particular model (AgentBasicInfo).
'manage.py makemigrations' works fine. 'manage.py migrate' also works fine.
'manage.py runserver' works fine, the whole website works fine until I try to go onto the admin page of this model.
The app is correctly installed in INSTALLED_APPS in settings.py. I am using Postgres for the database.
I have tried...
Deleting migrations and re-running makemigrations/migrate
Deleting the entire migrations folder for this app and rerunning makemigrations/migrate
Deleting all the migrations from all my apps and re-running makemigrations/migrate
I have tried running 'manage.py migrate' and 'mangae.py migrate app_name'. I still get the same error.
This model (see code below) is quite basic. I have several other models in my project and they work just fine in the admin, but just this particular model doesn't work.
models.py
class AgentBasicInfo(models.Model):
preferred_email = models.EmailField()
office_phone_number = models.IntegerField()
brokerage_of_agent = models.CharField(max_length=50)
agent_title = models.CharField(max_length=20)
def __str__(self):
return self.preferred_email
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'lagger123',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
0001_initial.py
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='AgentBasicInfo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('preferred_email', models.EmailField(max_length=254)),
('office_phone_number', models.IntegerField()),
('brokerage_of_agent', models.CharField(max_length=50)),
('agent_title', models.CharField(max_length=20)),
],
),
]
Output of manage.py showmigrations:
accounts
[X] 0001_initial
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
coresite
(no migrations)
databases
(no migrations)
manage_listings
[X] 0001_initial
search_listings
(no migrations)
sessions
[X] 0001_initial
teams
(no migrations)
Open db command line.
python manage.py dbshell
And try this
delete from django_migrations where app='app_name';
Then delete migration files and run migration commands.
I also had this problem and tried:
python manage.py dbshell
But then I got this error:
CommandError: You appear not to have the 'psql' program installed or on your path.
This was due windows not finding psql in my environment path.
As an alternative, you can get it done by reverting changes (that is if you had previous changes in you git repository.
For me I used this method:
git checkout <commit hash> (which did not have the error)
After that pull the changes:
git pull <remote> <branch>
Then finally:
git push origin main
Hope this helps for the ones with git repositories. I welcome any corrections.
I am an extreme newbie to Django and using shell. Hence please be gentle. I am working on a site where the owner has lost the relationship with the developer and hence passwords to the admin accounts (front-end and back-end). I am trying to create superusers for both but am having problems with the database. The site uses a PostgreSQL database. In the shell I activate the virtual environment and run my command:
python3 manage.py createsuperuser
The email address is requested and enter but receive the error after several lines of script.
django.db.utils.OperationalError: FATAL: password authentication failed for the user "xxx".
Do I need to somehow activate or enable the connection to the database before running the command?? Again really new and not trying to be the developer on the site- just trying to gain access and create users. Many thanks.
ADDITION BASED ON CONVERSATION BELOW
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'hci',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
except from base.py
from .base import *
DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = True
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
DATABASES['default'].update({
'NAME': 'xxx_hcidemo',
'USER': 'xxx_hcidemo',
'PASSWORD': 'xxxxxxxxxx',
'HOST': 'localhost',
})
BROKER_URL = 'redis://localhost:11201/0'
CELERY_RESULT_BACKEND = 'redis://localhost:11201/0'
entire demo.py file with password and username removed....
Solution was found! Many thx- the manage.py file was pointing at demo database - had to change to point at the production database. Completely agree - bad set-up but I now have access.
Probably You (or someone else) change password to database.
Look at Your DATABASES settings in Your settings.py file and update PASSWORD field.
EDIT:
Your manage.py use different database settings, than site. Remove if __name__ == '__main__': from manage.py file and try to add user.
BTW. It's very bad solution to differentiation prod env from dev env in this way.