-1

How to migrate changes in a database who's name is user defined. i tried giving

data = request.data
cursor = connection.cursor()
cursor.execute("create database " + data["database"])
DATABASES["default"]["NAME"] = data["database"]
call_command("makemigrations")
call_command("migrate")

but this doesn't work. is there any way to do it?

  • question not clear can explain more what are doing and what do you want. – NVS Jan 11 '19 at 07:16
  • call_commend is used for runtime and mostly in the testing in the testing and try and change commend to `call_command("./manage.py makemigrations")` and `call_command("./manage.py migrate")` – NVS Jan 11 '19 at 07:18
  • @NVS i am creating a database in runtime with a user defined name and trying to migrate the tables inside the database. I am able to create the database easily but not able to migrate the tables inside the db. is there any way i can do it? – Aaroosh Pandoh Jan 11 '19 at 07:34
  • ok try these commend which i commented up there i think this will work. – NVS Jan 11 '19 at 07:35
  • error is comming as Unknown command: './manage.py makemigrations' when i try that @NVS – Aaroosh Pandoh Jan 11 '19 at 07:36
  • actually it is taking database name as one mentioned inside settings.py only which is DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'temp', 'USER': 'root', 'PASSWORD': 'root', 'HOST': 'localhost', 'PORT': '3306', } } – Aaroosh Pandoh Jan 11 '19 at 07:39

1 Answers1

1
DATABASES.update({'users1': {
                'ENGINE': 'django.db.backends.mysql',
                'NAME': data["database"],
                'USER': 'root',
                'PASSWORD': 'root',
                'HOST': 'localhost',
                'PORT': '3306',
                'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0,
                'OPTIONS': {}, 'TIME_ZONE': None,
                'TEST': {'CHARSET': None, 'COLLATION': None, 'NAME': None, 'MIRROR': None
            }
            }})
            call_command("makemigrations")
            call_command('migrate', database='users1', interactive=False, skip_checks=True)

This code worked for me