django-tenant-schemas wont apply migration to tenant schema, only public - python

I have a multi-tenant django app using django-tenant-schemas.
There is an SiteConfig app:
settings.py:
TENANT_APPS = (
...
'siteconfig',
...
)
INSTALLED_APPS = (
...
'siteconfig',
...
)
But my latest migration on that app won't apply to my tenants:
$ ./manage.py migrate_schemas --shared
[standard:public] === Running migrate for schema public
[standard:public] Operations to perform:
[standard:public] Apply all migrations: account, admin, ... siteconfig, sites, socialaccount, tenant, utilities
[standard:public] Running migrations:
[standard:public] Applying siteconfig.0007_siteconfig_access_code...
[standard:public] OK
As you can see it is only applying the migration to the public schema, and not my tenants.
If I look at my tenant, it shows the migration there as unapplied:
$ ./manage.py tenant_command showmigrations
Enter Tenant Schema ('?' to list schemas): ?
public - localhost
test - test.localhost
Enter Tenant Schema ('?' to list schemas): test
account
[X] 0001_initial
[X] 0002_email_max_length
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add+
.
.
.
siteconfig
[X] 0001_initial
[X] 0002_auto_20200402_2201
[X] 0003_auto_20200402_2218
[X] 0004_auto_20200402_2233
[X] 0005_auto_20200403_0947
[X] 0006_auto_20200403_1528
[ ] 0007_siteconfig_access_code # <-- DIDN'T APPLY!
Why is it not applying to the tenant test and how can I get it to do that?

You are running
manage.py migrate_schemas --shared
Which migrates only public schema
You should run
manage.py migrate_schemas
According to documentation

Related

Test whether a Django migration has been applied to a DB

I have a django project, and I wish to update a MySQL database from a previous version of the codebase to be compatible with the current version.
I have all of the migrations for the project, but I do not know which was the last one applied to this alternate database.
I can try and determine this manually, but I was wondering if there was an easier automated way of finding which the first unapplied migration is.
You can work with the showmigrations command [Django-doc], this will generate a list of the migrations where it will check a box in case that migration has been applied, so:
python3 manage.py showmigrations
or for a specific app:
python3 manage.py showmigrations app_name
For example for auth, a possible result is:
$ python manage.py showmigrations auth
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
[X] 0010_alter_group_name_max_length
[ ] 0011_update_proxy_permissions
[ ] 0012_alter_user_first_name_max_length
This thus means that all migrations have been applied, except the last two.

How to apply django initial migration?

showmigrations shows that there is 31 available migrations:
# python3 manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_auto_20190114_1409
[X] 0003_auto_20190114_1410
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_auto_20190114_1409
authtoken
[X] 0001_initial
[X] 0002_auto_20160226_1747
[X] 0003_auto_20190114_1409
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
[X] 0003_auto_20190114_1409
exchange_delivery
[X] 0001_initial
[X] 0002_enlarge_phone_field
[X] 0003_unique_external_id
[X] 0004_add_warehouse
[X] 0005_add_delivery_point_type
[X] 0006_update_delivery_type_operating_mode
[X] 0007_add_delivery_point_region_model
[X] 0008_update_warehouse_exchange
[X] 0009_add_verbose_name_for_warehouse_and_add_delivery_point_banned_group
[X] 0010_add_active_flag_to_warehouse
[X] 0011_auto_20190114_1409
sessions
[X] 0001_initial
[X] 0002_auto_20190114_1410
volt
[X] 0001_initial
[X] 0002_auto_20190114_1410
How to apply all this migrations ? migrate shows that No migrations to apply
# python3 manage.py migrate
Operations to perform:
Synchronize unmigrated apps: corsheaders, export, opinion, volt.integration1c.delivery, custom_logger, event_listener, legacy, region, order, catalog, promo_table, messages, staticfiles, api, best_product, shop, general, market_cpa, rest_framework, delivery, exchange, talk
Apply all migrations: auth, authtoken, sessions, exchange_delivery, admin, volt, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
/usr/lib/python3.6/site-packages/django/core/management/commands/loaddata.py:239: RemovedInDjango19Warning: initial_data fixtures are deprecated. Use data migrations instead.
RemovedInDjango19Warning
Installed 0 object(s) (of 6) from 1 fixture(s)
Running migrations:
No migrations to apply.
I'm not familiar with Django, I need just dockerize existing app.
As far as I understand from output of python3 manage.py syncdb command
django.db.utils.ProgrammingError: (1146, "Table '220-django.auth_user' doesn't exist")
Problem is that initial migrations for django and django-admin are not applied
Project is under Django 1.8.14
As I can see, all the migrations has [X] beside their names. Means these migrations are already applied, as per documentation. If there is any un-applied migrations, then it will show up as [ ] beside the name of the migration. For more details(if the migrations has been applied or not), please check the django_migrations table in DB.
You have some entries in django_migrations table in your database so that migrate shows No migrations to apply.
To solve this, goto database console and run the following command
delete from django_migrations;
Or, directly go to the database table and delete all rows.
Then run
python manage.py migrate

Django Migrate Exclusion of Specific Models to Different Databases

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']

Django don't create new sqlite table for new app model

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

ProgrammingError at "url" relation "app_model" does not exist LINE 1: SELECT COUNT(*) AS "__count" FROM "app_model"

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.

Categories

Resources