Django change Migration table name in DB - django

How can I custom the django_migrations table name?
Even I edit the
venv.lib.site-packages.django.db.migrations.recorder > MigrationRecorder > meta = db_table = "ABC_django_migrations"
the makemigrations cannot detect the changes.
I am using Django version: 3.0.5

Django migrations don't look for changes in its own migration model
MigrationExecutor just ensures that following table exists in database
def migrate(self, targets, plan=None, state=None, fake=False, fake_initial=False):
self.recorder.ensure_schema()
....
where ensure_schema() just creates the table
def ensure_schema(self):
"""Ensure the table exists and has the correct schema."""
# If the table's there, that's fine - we've never changed its schema
# in the codebase.
if self.has_table():
return
# Make the table
try:
with self.connection.schema_editor() as editor:
editor.create_model(self.Migration)
except DatabaseError as exc:
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
you can manually make migration to edit this model ( AlterModelTable or custom sql) but I would not advise changing anything regarding migrations

How I solve this problem:
Install the app
pip install django-db-prefix
Include the apps in Settings.py
INSTALLED_APPS = ['django_db_prefix',]
Adding the prefix in Settings.py
DB_PREFIX = "foo_"

Related

Exclude database from django migrations

Is there a way to exclude database from django migrations?
I have a sphinxsearch database in my django project:
DATABASES['sphinxsearch'] = {
'ENGINE': 'sphinxsearch.backend.sphinx',
...
}
And when I try to run manage.py makemigrations command, Django tries to run
SHOW FULL TABLES query against it
which leads to an error, because this is wrong syntax for sphinxql
File "C:\Anaconda\lib\site-packages\django\db\backends\mysql\introspection.py", line 56, in get_table_list
cursor.execute("SHOW FULL TABLES")
...
django.db.utils.ProgrammingError: (1064, "sphinxql: syntax error, unexpected IDENT, expecting VARIABLES near 'FULL TABLES'")
An exception to this rule is the makemigrations command.
It validates the migration history in the databases to catch problems with the existing migration files (which could be caused by editing them) before creating new migrations.
By default, it checks only the default database, but it consults the allow_migrate() method of routers if any are installed.
makemigrations always creates migrations for model changes, but if allow_migrate() returns False, any migration operations for the model_name will be silently skipped when running migrate on the db.
Changing the behavior of allow_migrate() for models that already have migrations may result in broken foreign keys, extra tables, or missing tables. When makemigrations verifies the migration history, it skips databases where no app is allowed to migrate.
class DBMigrateRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Allows migration for default DB
"""
return db == 'default'
Add this class to settings file
DATABASE_ROUTERS = ['path.to.DBMigrateRouter']
This will skip migrations on databases other than "default".

right way to create a django data migration that creates a group?

I would like to create data migrations that create Permissions and Groups, so that my other developers can just run the migrations and get everything set up. I was able to create the migrations and run them just fine, but now I'm getting an error when running my tests.
But if I do this:
from django.contrib.auth.models import Group
def add_operations_group(apps, schema_editor):
Group.objects.get_or_create(name='operations')
I get:
django.db.utils.OperationalError: no such table: auth_group
If I do this:
def add_operations_group(apps, schema_editor):
Group = apps.get_model("django.contrib.auth", "group")
Group.objects.get_or_create(name='operations')
I get:
LookupError: No installed app with label 'django.contrib.auth'
Is there a way to do this? Or is there a "Django Way" to make sure things like permissions and groups are created?
This is how I do it:
from django.db import models, migrations
def apply_migration(apps, schema_editor):
Group = apps.get_model('auth', 'Group')
Group.objects.bulk_create([
Group(name=u'group1'),
Group(name=u'group2'),
Group(name=u'group3'),
])
def revert_migration(apps, schema_editor):
Group = apps.get_model('auth', 'Group')
Group.objects.filter(
name__in=[
u'group1',
u'group2',
u'group3',
]
).delete()
class Migration(migrations.Migration):
dependencies = [
('someapp', 'XXXX_some_migration'),
]
operations = [
migrations.RunPython(apply_migration, revert_migration)
]
Although, there must be a more Djangonic way.
Answer from César is correct. To make it more Django create the migration file automatically by going to your django app root folder and entering:
python manage.py makemigrations <yourappname> --empty
Note: You may need python3 instead of python depending on your system configuration.
This creates an empty migration file in a sub directory of your app called 0001_initial.py
You can then alter it as per César instructions. Which worked correctly with Django 2.2

App tables not created when running test

When i run my app test django don't create my app tables and throw an error.
My test file:
from django.test import TestCase
class MyTest(TestCase):
fixtures = ['initial_data.json']
def test_my_stuff(self):
[...]
When i run test:
DatabaseError: Problem installing fixture
'/home/.../djStock/stock/fixtures/initial_data.json': Could
not load stock.Provider(pk=1): (1146, "Table
'test_djstock.stock_provider' doesn't exist")
My app is correctly added in INSTALLED_APPS. What i miss ?
Must be have south migration files. Check if myapp/migration/ contain files for migration.
You have to comment out 'south' in INSTALLED_APPS before running any tests, otherwise if models are updated, you Django won't have the update reflected in the table creation, thus you get the error that table does not exist.
Assuming you are using Django 1.6.

Django manage.py - Creating auth_permission and django_content_type tables

I am unable to use syncdb because my app uses some MySQL views. I have run manage.py sqlall <app>, but this does not output the SQL for django_content_type table or the auth_permission tables. I have also had a look into south and django evolution, but they both require syncdb, and I'm not sure they would help anyway.
I have manually added some models to the tables, but this is getting frustrating, and having installed the dbsettings app I am unsure of what I now need to enter.
Does anyone know of a way to get manage.py (or something else) to output the SQL for these tables and their contents?
Thanks.
Having done a bit more digging, I found these:
Fixing the auth_permission table after renaming a model in Django and manage.py sql command for django models - Django.
These output the tables, but not the data:
python manage.py sql auth
python manage.py sql admin
But this gets a lot closer. In the end I managed it with the following:
from django.contrib.auth.management import create_permissions
from django.db.models import get_apps
for app in get_apps():
create_permissions(app, None, 2)
from django.contrib.contenttypes.management import update_all_contenttypes
update_all_contenttypes(interactive=True)
This adds all the permissions and then all the content types which are needed. interactive=True means that it asks you if you want to remove stale content types.
#hajamie solution works for older supported version, taking a hint, below is what worked for me!
django = 1.9.7
from django.contrib.auth.management import create_permissions
from django.contrib.auth.models import Permission
from django.apps import apps
def fix_user_permission():
"""
run this method via shell whenever any amendments in any of the tables is made
"""
print "fixing user permissions"
# delete pre-existing user permission
Permission.objects.all().delete()
apps.models_module = True
create_permissions(apps, verbosity=0)
apps.models_module = None
print "process completed - fixed user permissions"
The easiest solution I found is to install Django Extensions, add it to settings.INSTALLED_APPS and run:
manage.py update_permissions

Django db Table delete

With what command can I delete tables from a django db of a specific app/ only one table?
I did find all sorts of things, but not how to get rid of a table.
Also while I am at this.
I create the models and hit syncdb and then I have the tables.
If I want to update/add to those tables, do I run into problems?
Your best bet would be to get django-south installed in your machine.
if you are using pip, do pip install django-south
This allows you too migrate data forward and backwards.
This is very handy especially if you need to update tables, and new tables etc.
check it out.
adding south to apps are as easy as python manage.py schemamigration appname --initial
Make your changes in a model and run the following python manage.py schemamigration appname --auto
Once your data migration file has been created it'll tell you data is now ready to migrate.
Simply use python manage.py migrate appname
http://south.aeracode.org/docs/about.html
Hope this helps
If you are deleting a table, this is done in the model file of the specific app that you are trying to delete, there is no command for this, you just go into the file and delete it and then re-run syncdb, for your other question, the answer is the same.. every app folder should have a file called "models.py" and here is where the models which are, in this case, equivalent to tables are specified, along with their fields, you simply edit this to make any changes.
def reset():
import install
from django.db.models import get_models
removed_tables = []
exceptions = []
for model in get_models():
if model.__name__ not in ('User','Session','Group','Permission'):
try:
model.objects.all().delete() # So we can remove the table without complaints from the database server.
CURSOR.execute('DROP TABLE %s' % model._meta.db_table)
print "Dropped table %s from model %s" % (model._meta.db_table, model.__name__)
except Exception as e:
exceptions.append([model._meta.db_table, str(e)])
continue
removed_tables.append(model._meta.db_table)
print "Removed %s tables" % len(removed_tables)
syncdb()
install.install() # A function that leads to the creation of my default data