kobotoolbox / kpi

kpi is the server for KoboToolbox. It includes an API for users to access data and manage their forms, question library, sharing settings, create reports, and export data.
https://www.kobotoolbox.org
GNU Affero General Public License v3.0
137 stars 182 forks source link

error when i make python manage.py migrate #1047

Closed MohammedTA closed 5 years ago

MohammedTA commented 8 years ago

i got this error when am migrate db with this command python manage.py migrate

ValueError: Cannot quote parameter value <psycopg2._json.Json object at 0x7f265a585ad0> of type <class 'psycopg2._json.Json'>

i trying to run last version of KPI on ubuntu 16.04 python 2.7 psycopg2 2.6.1

any help ?

dorey commented 8 years ago

Which version of Postgres are you using?

The JSON fields are only available in Postgres 9.4 and above.

aymns commented 8 years ago

same error here psql (PostgreSQL) version : 9.4.10

Applying kpi.0015_assetversion...Traceback (most recent call last): File "manage.py", line 10, in execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 354, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 346, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv self.execute(*args, *cmd_options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute output = self.handle(args, **options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration state = migration.apply(state, schema_editor) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards field, File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field self._remake_table(model, create_fields=[field]) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table self.effective_default(field) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 46, in quote_value raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value))) ValueError: Cannot quote parameter value <psycopg2._json.Json object at 0x7feaf3c1fbd0> of type <class 'psycopg2._json.Json'>

aymns commented 8 years ago

@dorey , please any updates ?

MohammedTA commented 7 years ago

@dorey any updates ?

dorey commented 7 years ago

Are you still seeing this error on the latest master branch? Assuming you are using postgres 9.4+, @MohammedTA , then perhaps you can paste more context from the migration command so we can know at which step this is failing?

We are not seeing this problem inside our docker setup which builds with travis and uses postgres 9.4.

MohammedTA commented 7 years ago

full error @dorey

i want to install kobotoolbox manually without docker sudo python manage.py migrate Operations to perform: Synchronize unmigrated apps: loginas, mptt, webpack_loader, staticfiles, messages, markitup, debug_toolbar, django_extensions, rest_framework, haystack Apply all migrations: authtoken, oauth2_provider, reversion, hub, sessions, admin, auth, django_digest, contenttypes, taggit, registration, kpi Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying authtoken.0001_initial... OK Applying authtoken.0002_auto_20160226_1747... OK Applying django_digest.0001_initial... OK Applying hub.0001_initial... OK Applying hub.0002_formbuilderpreference... OK Applying hub.0003_auto_20160318_1808... OK Applying reversion.0001_squashed_0004_auto_20160611_1202... OK Applying taggit.0001_initial... OK Applying kpi.0001_initial... OK Applying kpi.0002_auto_20150619_0603... OK Applying kpi.0003_assetsnapshot... OK Applying kpi.0004_default_permissions_1910... OK Applying kpi.0005_taguid... OK Applying kpi.0006_importtask_messages... OK Applying kpi.0007_importtask_defaults... OK Applying kpi.0008_authorizedapplication... OK Applying kpi.0009_auto_20160315_0343... OK Applying kpi.0010_asset_deployment_data... OK Applying kpi.0011_explode_asset_deployments... migrated 0 assets !!! Only the most recent deployment of each asset has been !!! retained. Use the command./manage.py sync_kobocat_xforms !!! to create new assets for any orphaned KC forms. OK Applying kpi.0012_onetimeauthenticationkey... OK Applying kpi.0013_uid_field... OK Applying kpi.0014_discoverable_subscribable_collections... OK Applying kpi.0015_assetversion...Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 346, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute output = self.handle(*args, **options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration state = migration.apply(state, schema_editor) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards field, File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field self._remake_table(model, create_fields=[field]) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table self.effective_default(field) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 46, in quote_value raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value))) ValueError: Cannot quote parameter value <psycopg2._json.Json object at 0x7fe833c19e90> of type <class 'psycopg2._json.Json'>

raeesaroj commented 7 years ago

when i try to migrate

Operations to perform: Synchronize unmigrated apps: loginas, mptt, webpack_loader, staticfiles, messages, markitup, debug_toolbar, django_extensions, rest_framework, haystack Apply all migrations: authtoken, oauth2_provider, reversion, hub, sessions, admin, auth, django_digest, contenttypes, taggit, registration, kpi Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying kpi.0015_assetversion...Traceback (most recent call last): File "./manage.py", line 10, in execute_from_command_line(sys.argv) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/core/management/init.py", line 354, in execute_from_command_line utility.execute() File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/core/management/init.py", line 346, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv self.execute(*args, *cmd_options) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute output = self.handle(args, **options) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration state = migration.apply(state, schema_editor) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards field, File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field self._remake_table(model, create_fields=[field]) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table self.effective_default(field) File "/home/raee/Venvironment/kpi/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 46, in quote_value raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value))) ValueError: Cannot quote parameter value <psycopg2._json.Json object at 0x7f6c097ecc50> of type <class 'psycopg2._json.Json'>

esmail commented 7 years ago

I'm seeing some mention of django/db/backends/sqlite3 in your traceback, @raeesaroj. Is it possible you haven't set the DATABASE_URL environment variable?

For an example, see: https://github.com/kobotoolbox/kobo-docker/blob/80cc7b/envfiles/kpi.txt#L7

jonathanD00 commented 6 years ago

I'm also try to install Kobotoolbox/kpi without docker and I have the same error when I run python manage.py migrate. For information, the value of the psycopg2 json (who create th error) is : '{}' Do you have find any solution ?

Edit I have found a solution, but I think unsustainable. I edited the file kobo/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py Add in top file : from psycopg2.extras import json, Json

And replace this part: `class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):

sql_delete_table = "DROP TABLE %(table)s"
sql_create_inline_fk = "REFERENCES %(to_table)s (%(to_column)s)"

def quote_value(self, value):
    # The backend "mostly works" without this function and there are use
    # cases for compiling Python without the sqlite3 libraries (e.g.
    # security hardening).
    import _sqlite3
    try:
        value = _sqlite3.adapt(value)
    except _sqlite3.ProgrammingError:
        pass
    # Manual emulation of SQLite parameter quoting

    if isinstance(value, type(True)):
        return str(int(value))
    elif isinstance(value, (Decimal, float)):
        return str(value)
    elif isinstance(value, six.integer_types):
        return str(value)
    elif isinstance(value, six.string_types):
        return "'%s'" % six.text_type(value).replace("\'", "\'\'")
    elif value is None:
        return "NULL"
    elif isinstance(value, (bytes, bytearray, six.memoryview)):
        # Bytes are only allowed for BLOB fields, encoded as string
        # literals containing hexadecimal data and preceded by a single "X"
        # character:
        # value = b'\x01\x02' => value_hex = b'0102' => return X'0102'
        value = bytes(value)
        hex_encoder = codecs.getencoder('hex_codec')
        value_hex, _length = hex_encoder(value)
        # Use 'ascii' encoding for b'01' => '01', no need to use force_text here.
        return "X'%s'" % value_hex.decode('ascii')
    elif isinstance(value, Json): #My own parse
        print "WARNING PARSE JSON VALUE TO STR  ------------------------------"
        print value
        return str(value)
    else:
        raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value)))

` After that, python manage.py migrate is OK

awemulya commented 6 years ago

In my case the error was not finding database settings. in settings.py append try: from kobo.local_settings import * # nopep8 except ImportError: pass

In local_settings.py DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'db_name', 'USER': 'postgres', 'PASSWORD': '', 'HOST': '', 'PORT': '', } }

jnm commented 5 years ago

Thanks to everyone who posted guidance. https://community.kobotoolbox.org/c/kobo-developers would be a good place for further discussion.

sarveshwaranj commented 5 years ago

Have u guys solved this problem