jayvynl / django-clickhouse-backend

Django clickhouse database backend.
MIT License
130 stars 21 forks source link

[BUG] django_migrations.deleted #51

Closed oxpaoff closed 1 year ago

oxpaoff commented 1 year ago

Describe the bug i have such error while python manage.py migrate --database clickhouse - DB::Exception: There's no column 'django_migrations.deleted' in table 'django_migrations': While processing django_migrations.deleted: While processing SELECT django_migrations.id, django_migrations.app, django_migrations.name, django_migrations.applied, django_migrations.deleted FROM django_migrations WHERE NOT django_migrations.deleted. Stack trace: .

To Reproduce I do not know what changed by i just run python manage.py migrate --database clickhouse

Expected behavior No errors.

Versions

jayvynl commented 1 year ago

@oxpaoff I'am sorry, this is a bug. I have made an uncompatiable change, the minor version should be changed too!

for now, you can fix this by add deleted column manually.

connect to your clickhouse server and run:

ALTER table django_migrations ADD COLUMN deleted Bool;
Song-yes commented 1 year ago

Excuse me, I also seem to have encountered a problem about 'deleted', I thought it was my own code problem at first, but then I directly created a new djangorestframework project, just did the operation of configuring the clickhouse database, and found that the error was the same as the test project I wrote, I suspected it was a version update problem, the following is my error message: (I also found that when executing python manage.py makemigrations, there are no files generated in the migrations directory, I want to try to test the user functionality built into DjangorestFramework, I think there should be no need to build models, so I don't have a configuration models.py file.)

(venv) PS D:\source\djangoProject1> python manage.py makemigrations No changes detected (venv) PS D:\source\djangoProject1> python manage.py migrate
Traceback (most recent call last): File "D:\source\djangoProject1\manage.py", line 22, in main() File "D:\source\djangoProject1\manage.py", line 18, in main execute_from_command_line(sys.argv) File "D:\source\djangoProject1\venv\Lib\site-packages\django\core\management__init__.py", line 442, in execute_from_command_line utility.execute() File "D:\source\djangoProject1\venv\Lib\site-packages\django\core\management__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\source\djangoProject1\venv\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv self.execute(*args, cmd_options) File "D:\source\djangoProject1\venv\Lib\site-packages\django\core\management\base.py", line 458, in execute output = self.handle(*args, *options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\core\management\base.py", line 106, in wrapper res = handle_func(args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\core\management\commands\migrate.py", line 120, in handle executor.loader.check_consistent_history(connection) File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\migrations\loader.py", line 313, in check_consistent_history applied = recorder.applied_migrations() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\migrations\recorder.py", line 84, in applied_migrations for migration in self.migration_qs ^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\clickhouse_backend\patch\migrations.py", line 70, in migration_qs return self.Migration.objects.using(self.connection.alias).filter( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\models\query.py", line 1436, in filter return self._filter_or_exclude(False, args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\models\query.py", line 1454, in _filter_or_exclude clone._filter_or_exclude_inplace(negate, args, kwargs) File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\models\query.py", line 1461, in _filter_or_exclude_inplace self._query.add_q(Q(*args, **kwargs)) File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\models\sql\query.py", line 1545, in addq clause, = self._add_q(q_object, self.used_aliases) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\models\sql\query.py", line 1576, in _add_q child_clause, needed_inner = self.build_filter( ^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\models\sql\query.py", line 1426, in build_filter lookups, parts, reffed_expression = self.solve_lookuptype(arg, summarize) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ , field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\source\djangoProject1\venv\Lib\site-packages\django\db\models\sql\query.py", line 1724, in names_to_path raise FieldError( django.core.exceptions.FieldError: Cannot resolve keyword 'deleted' into field. Choices are: app, applied, id, name

Song-yes commented 1 year ago

@oxpaoff I'am sorry, this is a bug. I have made an uncompatiable change, the minor version should be changed too!

for now, you can fix this by add column manually.deleted

connect to your clickhouse server and run:

ALTER table django_migrations ADD COLUMN deleted Bool;

By the way, even if I executed this SQL, it still didn't work.

jayvynl commented 1 year ago

@oxpaoff I'am sorry, this is a bug. I have made an uncompatiable change, the minor version should be changed too! for now, you can fix this by add column manually.deleted connect to your clickhouse server and run:

ALTER table django_migrations ADD COLUMN deleted Bool;

By the way, even if I executed this SQL, it still didn't work.

  1. Could you show the result of show create table django_migrations; ? I'm using v1.1.2 in my own project and adding deleted column have solved this problem.
  2. You are discouraged to use clickhouse to deal with authentication/authrozation. See this.
Song-yes commented 1 year ago

Sure! CREATE TABLE django_data.django_migrations ( id Int64, app FixedString(255), name FixedString(255), applied DateTime64(6, 'UTC'), deleted Bool ) ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 8192

jayvynl commented 1 year ago

@Song-yes I guess this is another bug, when you don't import clickhouse_backend.models in project, then clickhouse_backend.patch.patch_all is not called at the right time. I haven't tested without using ClickhouseModel.

For now, your can:

  1. create an app
    python manage.py startapp stub
  2. edit stub/models.py, add this line
    import clickhouse_backend.models
  3. add stub to INSTALLED_APPS in your django settings file
Song-yes commented 1 year ago

Oh, I didn't take into account, for models files without configuration, also need to import modules, thank you very much for your answer。