How to Alter sequence for whole DB in Postgres - django

I'm working with Django + PostgreSQL and I restored backup from my old DB dump on a current DB due to some reasons. Due to which the sequences of few tables were disturbed and I am not able to create a new Django superuser because of that. Can someone help me with how can I alter sequence of the whole db using any psql command.
Error traceback if I've misinterpreted the error:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/contrib/auth/models.py", line 168, in create_superuser
return self._create_user(username, email, password, **extra_fields)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/contrib/auth/models.py", line 151, in _create_user
user.save(using=self._db)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/models/base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/models/base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/models/base.py", line 947, in _do_insert
using=using, raw=raw)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/models/query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/orion/workspace/buddy/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_user_pkey"
DETAIL: Key (id)=(1198) already exists.

Related

Django | Factory boy | faking a boolean field | django.db.utils.DataError: value too long for type character varying(1)

I am trying to use factory boy to generate fake entries but I'm stepping on an issue related with the boolean field.
Follows the Model and ModelFactory:
# models.py
class Record(models.Model):
date_creation = models.DateTimeField()
rec_type = models.CharField(max_length=1, choices=RECORD_TYPES)
direction = models.BooleanField()
# factories.py
class RecordFactory(DjangoModelFactory):
class Meta:
model = Record
date_creation = factory.Faker('date_time')
rec_type = factory.Faker('random_choices', elements=[x[1] for x in Record.RECORD_TYPES])
direction = factory.Faker('pybool')
How do I fix this issue? Seems to be related with the boolean field.
/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py:1427: RuntimeWarning: DateTimeField Record.date_creation received a naive datetime (1
977-11-24 14:21:26) while time zone support is active.
RuntimeWarning)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(1)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 30, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/app/iur/core/management/commands/seed.py", line 60, in handle
self.create(options["records"], RecordFactory)
File "/app/iur/core/management/commands/seed.py", line 47, in create
factory_class.create()
File "/usr/local/lib/python3.7/site-packages/factory/base.py", line 564, in create
return cls._generate(enums.CREATE_STRATEGY, kwargs)
File "/usr/local/lib/python3.7/site-packages/factory/django.py", line 141, in _generate
return super(DjangoModelFactory, cls)._generate(strategy, params)
File "/usr/local/lib/python3.7/site-packages/factory/base.py", line 501, in _generate
return step.build()
File "/usr/local/lib/python3.7/site-packages/factory/builder.py", line 279, in build
kwargs=kwargs,
File "/usr/local/lib/python3.7/site-packages/factory/base.py", line 315, in instantiate
return self.factory._create(model, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/factory/django.py", line 185, in _create
return manager.create(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 908, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(1)
It might not be the boolean field that cause the error but
rec_type = models.CharField(max_length=1, choices=RECORD_TYPES)
As it's max_length is 1. Can you confirm that RECORD_TYPES are max 1 character?
django.db.utils.DataError: value too long for type character varying(1)
This should indicate that it's a VARCHAR of max length 1. And from what I can see from the trace, you're db backend is postgres where there is a native boolean field.
The major issue was that I was using the wrong Faker. This one did what I expected:
# factories.py
...
rec_type = factory.Faker('random_element', elements=[x[0] for x in Record.RECORD_TYPES])
...
Just set e.g. direction = False in RecordFactory. Or, if you want to use faker, you can do direction = Faker().pybool()
I think the error is telling you that your DB backend uses a one letter varchar for representing booleans (most probably "t" and "f").

Permission denied for relation django_migrations using Heroku

While attempting to migrate database changes using django on Heroku, I get:
psycopg2.ProgrammingError: permission denied for relation django_migrations
Other people have been able to resolve this issue by granting the appropriate privileges (e.g., Permission denied for relation). Unfortunately, I can't grant permissions or create a new user on the Heroku hobby tier.
I was able to roll back a few migrations, but eventually I got:
django.db.utils.ProgrammingError: table "labs_branch_tests" does not exist
and could not migrate back to newer migrations.
Full traceback for permission denied for relation django_migrations
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 250, in apply_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 73, in record_applied
self.migration_qs.create(app=app, name=name)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: permission denied for relation django_migrations
Full traceback for psycopg2.ProgrammingError: table "labs_branch_tests" does not exist
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 119, in migrate
state = self._migrate_all_backwards(plan, full_plan, fake=fake)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 194, in _migrate_all_backwards
self.unapply_migration(states[migration], migration, fake=fake)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 264, in unapply_migration
state = migration.unapply(state, schema_editor)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 178, in unapply
operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 95, in database_backwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 457, in remove_field
return self.delete_model(field.remote_field.through)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 324, in delete_model
"table": self.quote_name(model._meta.db_table),
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: table "labs_branch_tests" does not exist
I was able to get around this by granting privileges to the user in the postgres terminal.
First connect to postgres through the Heroku CLI (heroku pg:psql).
This is clunky but get your Heroku postgres username by calling \c in the postgres terminal. (The last line of output will be You are now connected to database DATABASE_NAME as user USERNAME.)
Then grant privileges as others have shown:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public to USERNAME;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to USERNAME;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public to USERNAME;
In my case this was because my free database reached its row limit and write access was revoked:
$ heroku pg:info
=== DATABASE_URL
Plan: Hobby-dev
Status: Available
Connections: 0/20
PG Version: 10.4
[..]
Rows: 11683/10000 (Write access revoked)
[..]
for me yes you can tell the write access was revoked with
$ heroku pg:info
...
Rows: 11683/10000 (Write access revoked)
but I didn't want to dump the whole database. Instead I identify the biggest tables with
$ heroku pg:psql
> select
table_name
, pg_relation_size(quote_ident(table_name)) as "raw_size"
, pg_size_pretty(pg_relation_size(quote_ident(table_name)) as "size"
from information_schema.tables
where table_schema = 'public'
order by "raw_size" desc;
and then I clear the biggest tables with
> delete from <table name>;

django.db.utils.ProgrammingError: Could not load : column "" of relation "" does not exist

I just added a field to my model and added the values of the field to my fixtures. However, I am getting this error:
django.db.utils.ProgrammingError: Problem installing fixture 'app/fixtures/tool.json': Could not load "": column "new_field" of relation "app_model" does not exist
which is the same error I got before even putting values in the fixture. What is it that I'm forgetting?
Here's the Traceback:
Traceback (most recent call last):
File "./manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 64, in handle
self.loaddata(fixture_labels)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 104, in loaddata
self.load_label(fixture_label)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 167, in load_label
obj.save(using=self.using)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 201, in save
models.Model.save_base(self.object, using=using, raw=True, **kwargs)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/models/base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/models/base.py", line 889, in _save_table
forced_update)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/models/base.py", line 939, in _do_update
return filtered._update(values) > 0
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/models/query.py", line 654, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1148, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/webapps/my_app/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: Problem installing fixture '/webapps/my_app/tools/fixtures/tools.json': Could not load myapp.model(pk=3): column "new_field" of relation "myapp_model" does not exist
LINE 1: ..._LOCK,android.permission.RECEIVE_BOOT_COMPLETED', "new_field"...
So the way I solved this issue does not make so much sense. I did it by manually adding the column to the table. Then I ran migrate. Since I had the column the second time I was migrating, the error changed to column already exists. So then I deleted the column and it created the column automatically. I am not sure why it did not work in the first place, but if you have this problem, tweak around with the database.

Checking for empty queryset

I want to confirm is this the right way to check for empty queryset, and if it is why am I having a UNIQUE constraint error.
syn_check= Synonym.objects.filter(MD.objects.get(**filter_dict), synonym_type=Stype.objects.filter(description=values.capitalize().strip()), synonym_name=key)
if not syn_check:
print 'unavailable synonym', key, values
syn = Synonym()
syn.synonym_type=Stype.objects.filter(description=values.capitalize().strip()
syn.synonym_name = key.strip()
syn.save()
Am I doing anything that is weird? I also used if not syn_check.count() and I had the same problem.
Here is the traceback :
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/project/PycharmProjects/project/project/management/commands/back_populate_data.py", line 53, in handle
loaded_syn = data_loaded_syn4_pubchem(synonym_decoded, filter_dict)
File "/home/project/PycharmProjects/project/project/lookup_data.py", line 203, in data_loaded_syn4_pubchem
mol_syn_ob.save()
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/base.py", line 589, in save
force_update=force_update, update_fields=update_fields)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/base.py", line 698, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/base.py", line 731, in _do_insert
using=using, raw=raw)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 921, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 921, in execute_sql
cursor.execute(sql, params)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/project/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: project_synonym.molecule_id, project_synonym.synonym_name, project_synonym.synonym_type_id
Thanks
Checking the queryset as you are doing is fine.
if not syn_check:
However, it is not as efficent as it could be, you are loading all the objects from the database, when all you want to know is whether the queryset is empty or not.
It would be better to use count(),
if not syn_check.count():
and even better to use exists()
if not syn_check.exists():
The problem in your code is where you define the queryset
syn_check = Synonym.objects.filter(MD.objects.get(**filter_dict), synonym_type=Stype.objects.filter(description=values.capitalize().strip()), synonym_name=key)
The filter() method accepts either Q objects as args, or keyword arguments. It doesn't make sense to pass the model instance from MD.objects.get(...) as a positional argument.

Hue installation issue

I am new to bigdata technologies/hadoop ecosystem.
As part of one of my assignment I am trying to install and run Hue on my single node hadoop cluster [apache distribution hadoop2.6.0].
I have installed hue as per the instructions provided by many websites:
Downloaded latest hue tar file
Unzipped it at one location
Ran sudo make install
It installed hue in /usr/local/hue directory
Updated hue.ini file with required details of my cluster.
When I am trying to access the newly installed hue through web UI using ':8888', it is showing me following error page:
Traceback (most recent call last):
File "/usr/local/hue/desktop/core/src/desktop/lib/wsgiserver.py", line
1198, in communicate
req.respond()
File "/usr/local/hue/desktop/core/src/desktop/lib/wsgiserver.py", line 568,
in respond
self._respond()
File "/usr/local/hue/desktop/core/src/desktop/lib/wsgiserver.py", line 580,
in _respond
response = self.wsgi_app(self.environ, self.start_response)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/core/handlers/wsgi.py",
line 206, in call
response = self.get_response(request)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/core/handlers/base.py",
line 194, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/core/handlers/base.py",
line 236, in handle_uncaught_exception
return callback(request, **param_dict)
File "/usr/local/hue/desktop/core/src/desktop/views.py", line 304, in serve_500_error
return render("500.mako", request, {'traceback': traceback.extract_tb(exc_info[2])})
File "/usr/local/hue/desktop/core/src/desktop/lib/django_util.py", line
225, in render
**kwargs)
File "/usr/local/hue/desktop/core/src/desktop/lib/django_util.py", line
146, in _render_to_response
return django_mako.render_to_response(template, *args, **kwargs)
File "/usr/local/hue/desktop/core/src/desktop/lib/django_mako.py", line
125, in render_to_response
return HttpResponse(render_to_string(template_name, data_dictionary), **kwargs)
File "/usr/local/hue/desktop/core/src/desktop/lib/django_mako.py", line
114, in render_to_string_normal
result = template.render(**data_dict)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Mako-0.8.1-py2.6.egg/mako/template.py",
line 443, in render
return runtime.render(self, self.callable, args, data)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Mako-0.8.1-py2.6.egg/mako/runtime.py",
line 786, in _render
**_kwargs_for_callable(callable_, data))
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Mako-0.8.1-py2.6.egg/mako/runtime.py",
line 818, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Mako-0.8.1-py2.6.egg/mako/runtime.py",
line 844, in _exec_template
callable_(context, *args, **kwargs)
File "/tmp/tmpjqe8jG/desktop/500.mako.py", line 103, in render_body
M_writer(unicode( commonfooter(messages) ))
File "/usr/local/hue/desktop/core/src/desktop/views.py", line 388, in commonfooter
hue_settings = Settings.get_settings()
File "/usr/local/hue/desktop/core/src/desktop/models.py", line 59, in get_settings
settings, created = Settings.objects.get_or_create(id=1)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/manager.py",
line 154, in get_or_create
return self.get_queryset().get_or_create(**kwargs)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/query.py",
line 391, in get_or_create
six.reraise(*exc_info)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/query.py",
line 383, in get_or_create
obj.save(force_insert=True, using=self.db)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/base.py",
line 545, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/base.py",
line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/base.py",
line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/base.py",
line 687, in _do_insert
using=using, raw=raw)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/manager.py",
line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/query.py",
line 1514, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/models/sql/compiler.py",
line 903, in execute_sql
cursor.execute(sql, params)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/utils.py",
line 99, in __exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/usr/local/hue/build/env/lib/python2.6/site-packages/Django-1.6.10-py2.6.egg/django/db/backends/sqlite3/base.py", line 452, in execute
return Database.Cursor.execute(self, query, params)
OperationalError: attempt to write a readonly database
Not sure what does it mean, please help me in resolving this issue
Regards,
Bhupesh
Got it :-)
I was facing this issue because of the hue directory ownership.
I changed the owner of my /usr/local/hue folder :
$ sudo chown -R hue:hue /usr/local/hue
and then tried accessing the URL, it worked. :-)
//Bhupesh