djongo icon indicating copy to clipboard operation
djongo copied to clipboard

NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None

Open MikeTeddyOmondi opened this issue 4 years ago • 36 comments

Error occurs while performing an app specific makemigration command

NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None

Python script

I am running a simple migration for a model and it eventually creates the migration file 0001_initial.py but I wanted to know what's the meaning of this error and how it can be tackled.

python manage.py makemigrations api

Traceback

Migrations for 'api': api/migrations/0001_initial.py - Create model Task Traceback (most recent call last): File "manage.py", line 22, in main() File "manage.py", line 18, in main execute_from_command_line(sys.argv) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/init.py", line 401, in execute_from_command_line utility.execute() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/init.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 341, in run_from_argv connections.close_all() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/utils.py", line 230, in close_all connection.close() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 295, in close self._close() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/base.py", line 208, in _close if self.connection: File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/pymongo/database.py", line 829, in bool raise NotImplementedError("Database objects do not implement truth " NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None

MikeTeddyOmondi avatar Dec 11 '21 09:12 MikeTeddyOmondi

Error occurs after migrate command too

Python script

python manage.py migrate

Traceback

Operations to perform: Apply all migrations: admin, api, auth, contenttypes, sessions Running migrations: This version of djongo does not support "NULL, NOT NULL column validation check" fully. Visit https://nesdis.github.io/djongo/support/ Applying contenttypes.0001_initial...This version of djongo does not support "schema validation using CONSTRAINT" fully. Visit https://nesdis.github.io/djongo/support/ OK Applying auth.0001_initial...This version of djongo does not support "schema validation using KEY" fully. Visit https://nesdis.github.io/djongo/support/ This version of djongo does not support "schema validation using REFERENCES" fully. Visit https://nesdis.github.io/djongo/support/ OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying api.0001_initial... OK Applying contenttypes.0002_remove_content_type_name...This version of djongo does not support "COLUMN DROP NOT NULL " fully. Visit https://nesdis.github.io/djongo/support/ This version of djongo does not support "DROP CASCADE" fully. Visit https://nesdis.github.io/djongo/support/ Traceback (most recent call last): File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 857, in parse return handler(self, statement) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 894, in _alter query.execute() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 533, in _drop_column self.db[self.left_table].update( File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/pymongo/collection.py", line 2584, in call raise TypeError("'Collection' object is not callable. If you meant to " TypeError: 'Collection' object is not callable. If you meant to call the 'update' method on a 'Collection' object it is failing because no such method exists.

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

Traceback (most recent call last): File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute self.result = Query( File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 784, in init self._query = self.parse() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 885, in parse raise exe from e djongo.exceptions.SQLDecodeError:

    Keyword: None
    Sub SQL: None
    FAILED SQL: ALTER TABLE "django_content_type" DROP COLUMN "name" CASCADE
    Params: ()
    Version: 1.3.6

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

Traceback (most recent call last): File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute raise db_exe from e djongo.database.DatabaseError

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

Traceback (most recent call last): File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv self.execute(*args, **cmd_options) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute output = self.handle(*args, **options) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle post_migrate_state = executor.migrate( File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration state = migration.apply(state, schema_editor) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 178, in database_forwards schema_editor.remove_field(from_model, from_model._meta.get_field(self.name)) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 517, in remove_field self.execute(sql) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute cursor.execute(sql, params) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute return super().execute(sql, params) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute raise db_exe from e django.db.utils.DatabaseError

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "manage.py", line 22, in main() File "manage.py", line 18, in main execute_from_command_line(sys.argv) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/init.py", line 401, in execute_from_command_line utility.execute() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/init.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 341, in run_from_argv connections.close_all() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/utils.py", line 230, in close_all connection.close() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 295, in close self._close() File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/djongo/base.py", line 208, in _close if self.connection: File "/home/picasso/src/code_practice/python/todo_django_api/env/lib/python3.8/site-packages/pymongo/database.py", line 829, in bool raise NotImplementedError("Database objects do not implement truth " NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None

MikeTeddyOmondi avatar Dec 11 '21 09:12 MikeTeddyOmondi

How comes there are still unapplied migrations and I ran the migrate command?

Python Script

python manage.py runserver

Console

You have 12 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them.

December 11, 2021 - 10:15:14 Django version 3.0.3, using settings 'todo_django_api.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

MikeTeddyOmondi avatar Dec 11 '21 10:12 MikeTeddyOmondi

Yes, I am also facing the same issues

hanzala-sohrab avatar Dec 12 '21 02:12 hanzala-sohrab

Using pymongo 3.12.1 will solve your problem,I have faced this problem one hours ago

dzyt82 avatar Dec 12 '21 06:12 dzyt82

Thanks @dzyt82

hanzala-sohrab avatar Dec 12 '21 09:12 hanzala-sohrab

I found a workaround, when using the call_command, I do not see this error:

NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None

I am not sure why it's working, but maybe it could be helpful.

Python script

from django.core.management import call_command
call_command("migrate")

I am using: pymongo==4.0.1 djongo==1.3.6

cruky avatar Jan 18 '22 13:01 cruky

@cruky Where do you place this script in a project??

MikeTeddyOmondi avatar Jan 22 '22 10:01 MikeTeddyOmondi

@dzyt82 I see that version of Pymongo is very healthy, according to Snyk. I'll use that and see.

MikeTeddyOmondi avatar Jan 22 '22 10:01 MikeTeddyOmondi

@MikeTeddyOmondi Hey , have you resolved this issue? , actually I m stucking too into this and not getting any exact solution.

AmrapaliKumari avatar Jan 27 '22 11:01 AmrapaliKumari

@AmrapaliKumari I haven't gotten through the issue since the last recommended step...The server keeps printing that log anytime I do a migration. I'm still waiting for it to be addressed.

MikeTeddyOmondi avatar Jan 27 '22 14:01 MikeTeddyOmondi

@MikeTeddyOmondi okk Thanks, Is there any other way to integrate MongoDB with Django bcoz according to Django documentation I have tried but still no luck , not getting any other way to restart the project?

AmrapaliKumari avatar Jan 28 '22 03:01 AmrapaliKumari

Bump, same error. Is anyone figured out how to solve it?

aurok11 avatar Feb 02 '22 12:02 aurok11

@aurok11 Hii, m also facing the same error, try to upgrade or downgrade the versions of djongo and django hope this will help you because earlier I m using django latest version downgrading both djongo and django solved my error.

AmrapaliKumari avatar Feb 03 '22 05:02 AmrapaliKumari

Using pymongo 3.12.1 will solve your problem,I have faced this problem one hours ago

This works for me. Thx.

kemen209 avatar Feb 04 '22 14:02 kemen209

So devs apparently it has been a while I touched the project but I found out downgrading the pymongo to version 3.12.1 works great as suggested by @dzyt82 and in addition, it has good package health according to Snyk; and also while I do a python manage.py migrate and when I perform any CRUD operations it doesn't bring the errors in the console as stated earlier before in the issue.

After Migration:

image

Now all the migrations are applied and no errors occur when doing CRUD operations:

image

Requirements.txt file contents looks like this:

image

MikeTeddyOmondi avatar Feb 04 '22 14:02 MikeTeddyOmondi

So devs apparently it has been a while I touched the project but I found out downgrading the pymongo to version 3.12.1 works great as suggested by @dzyt82 and in addition, it has good package health according to Snyk; and also while I do a python manage.py migrate and when I perform any CRUD operations it doesn't bring the errors in the console as stated earlier before in the issue.

After Migration:

image

Now all the migrations are applied and no errors occur when doing CRUD operations:

image

Requirements.txt file contents looks like this:

image

hi , the djongo adapter doesn't seem to work with django 4.0, any particular reason why you used django 3.0.3? as that's a lil bit old versioned. won't this cause security risk? if there were vulnerabilities in this other version of django?

Jephpico avatar Feb 09 '22 15:02 Jephpico

Hii , Can anyone suggest me 2 or 3 sample projects using django integration with MongoDB database as i was doing using default sqlite . But want to try with using mongoDB

AmrapaliKumari avatar Feb 09 '22 15:02 AmrapaliKumari

Hi @Jephpico check this issue issue 594 I addressed it there....

MikeTeddyOmondi avatar Feb 09 '22 15:02 MikeTeddyOmondi

Todo or Blog RESTful API @AmrapaliKumari

MikeTeddyOmondi avatar Feb 09 '22 15:02 MikeTeddyOmondi

@MikeTeddyOmondi any youtube channel or any site for reference ?

AmrapaliKumari avatar Feb 09 '22 17:02 AmrapaliKumari