Hello everyone, I am running a django project with...
# general
b
Hello everyone, I am running a django project with pants and I seem not to have migrations consistently updated in my postgres database. That is sometimes it works and other it shows no migrations to apply yet I have run
./pants run <path to file>:manage -- makemigrations accounts
which shows migrations have happened but when I run
./pants run <path to file>:manage -- migrate
or
./pants run <path to file>:manage -- migrate accounts
where accounts is the name of the app the models are located, it shows that
Copy code
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
h
As a side note, why are you adding
:manage
to the target? You should be able to do
pants run path/to/manage.py
Or are you doing
pants run path/to:manage
(i.e., using the address of a pex_binary target)?
And to clarify your main question: when you run
pants run <path to file>:manage -- makemigrations accounts
it says it has created a new migration file? Does that new file show up in your source tree? (for example does
git status
show it as a new file?)
I assume not, because
pants run
runs files in a sandbox, so the new migration gets created there but doesn't get copied back to the real location in the source tree. So when you
pants run
to apply the migration, there is no new migration available.
To fix this, set
run_goal_use_sandbox=False
on the
manage.py
. Which you can do with an override:
Copy code
python_sources(
    overrides={
        "manage.py": {
            "run_goal_use_sandbox": False,
            "restartable": True,
        },
    },
(restartable lets you run the Django dev server in a loop)
I realize we did not add this in the example-django repo
@brash-area-42337 if this works for you, we'd really appreciate a PR to add the run_goal_use_sandbox setting in the 4 services under https://github.com/pantsbuild/example-django/tree/main/helloworld/service ! That way future readers will have a reference.
b
I am actually running
./pants run path/to/file:manage
as an address to the pex binary target this below is my pex_binary for manage.py
Copy code
pex_binary(
    name="manage",
    entry_point="manage.py",
    dependencies=[
        "fleetdatabaseFinal:fleetdatabaseFinal",
        "accounts:accounts",
        "3rdparty:reqs#psycopg2-binary",
        "3rdparty:reqs#gunicorn",
        "3rdparty:reqs#djangorestframework",
    ],
    restartable=True,
    inherit_path="prefer",
)
let me set
run_goal_use_sandbox=false
the same way I set the restartable
Let me try it out and will be glad to do a PR after adding the
run_goal_use_sandbox
setting in the 4 services in the repo
Can I still do the overide with such a pex_binary
I have tried it out but the migrations file still does not appear in the source control tree, and the changes still reflect
This is what I still get when I try to apply the made migrations
Copy code
kennan@medad:~/NPA/fleet-backend$ ./pants run fleetdatabaseFinal:manage -- migrate
System check identified some issues:

WARNINGS:
accounts.CustomUser.groups: (fields.W340) null has no effect on ManyToManyField.
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
Actually the migration files are not updated
h
Try
pants run path/to/manage.py
instead of using the address and set
run_goal_use_sandbox=false
on the source file, as I showed in the example (if that does change the migration file then I'll explain the difference between the two...)
b
When I run
pants run path/to/manage.py
I get this exception below yet I included the 'python-dotenv' package in the requirements file
Copy code
kennan@medad:~/NPA/fleet-backend$ ./pants run fleetdatabaseFinal/manage.py -- makemigrations
Traceback (most recent call last):
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/management/base.py", line 443, in execute
    self.check()
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/management/base.py", line 475, in check
    all_issues = checks.run_checks(
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/checks/caches.py", line 64, in check_file_based_cache_is_absolute
    for alias, config in settings.CACHES.items():
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/conf/__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/kennan/NPA/fleet-backend/fleetdatabaseFinal/settings.py", line 101, in <module>
    from dotenv import load_dotenv
ModuleNotFoundError: No module named 'dotenv'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/pex", line 236, in <module>
    runpy.run_module(module_name, run_name="__main__", alter_sys=True)
  File "/usr/lib/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/kennan/NPA/fleet-backend/fleetdatabaseFinal/manage.py", line 21, in <module>
    main()
  File "/home/kennan/NPA/fleet-backend/fleetdatabaseFinal/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/core/management/base.py", line 415, in run_from_argv
    connections.close_all()
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/utils/connection.py", line 84, in close_all
    for conn in self.all(initialized_only=True):
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/utils/connection.py", line 76, in all
    return [
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/utils/connection.py", line 73, in __iter__
    return iter(self.settings)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/utils/connection.py", line 45, in settings
    self._settings = self.configure_settings(self._settings)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/db/utils.py", line 148, in configure_settings
    databases = super().configure_settings(databases)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/utils/connection.py", line 50, in configure_settings
    settings = getattr(django_settings, self.settings_name)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/conf/__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/kennan/.cache/pants/named_caches/pex_root/venvs/c327cad8531010afb442b743400c471a24792439/ee3bffdf8d6817038d2c7bb7f11bbf094d378a77/lib/python3.9/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/kennan/NPA/fleet-backend/fleetdatabaseFinal/settings.py", line 101, in <module>
    from dotenv import load_dotenv
ModuleNotFoundError: No module named 'dotenv'
It does the same when you try to import '*rest_framework_simplejwt*' yet we have '*djangorestframework-simplejwt'* included in requirements.txt file.