简体   繁体   中英

How to read data from an External Server and apply migrations on a local Db file in Django?

My situation is as follows:

I have an external database with a read-only access from where I want to import my Data Models, which I did using:

python manage.py inspectdb > models.py

After I have handled all my exceptions after importing the models, when I try to migrate, it asks for permission to write to the external db to create extra tables for running the django project, like admin, auth, contenttypes, sessions

How do I continue reading data from my external database for running my REST API while applying the required migrations for Auth and other important tables to another locally stored database?

Following is the error I'm getting:

LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...
                     ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\migrations\recorder.py", line 67, in ensure_schema
    editor.create_model(self.Migration)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\backends\base\schema.py", line 307, in create_model
    self.execute(sql, params or None)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\backends\base\schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 99, in execute
    return super().execute(sql, params)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: permission denied for schema public
LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...
                     ^


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\core\management\commands\migrate.py", line 232, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\migrations\executor.py", line 91, in migrate
    self.recorder.ensure_schema()
  File "C:\Users\prateek.jain\Anaconda3\lib\site-packages\django\db\migrations\recorder.py", line 69, in ensure_schema
    raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (permission denied for schema public
LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...
                     ^
)

Check out multi-db support in django. You can route your django migrations like admin, auth, contenttypes,.. etc , to local database. and use remote read-only database for reads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM