I'm trying to run the test for my application, Its giving messsage like
django.db.utils.ProgrammingError: column "status" cannot be cast automatically to type integer
HINT: Specify a USING expression to perform the conversion.
Oh, Its because i have altered the CharField to IntegerField using SQL syntax (Although migration was created for this change)
ALTER TABLE accounts ALTER COLUMN status TYPE integer USING (status::integer);
So when running the test command, is failing while creating the test database.
Is there any way around to this ?
Update:
Right now i'm skipping the part of running migrations using third party app using https://pypi.python.org/pypi/django-test-without-migrations/
Now test tables are being created but failing for a cache table
File "/home/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 229, in sync_apps
sql, references = connection.creation.sql_create_model(model, no_style(), seen_models)
File "/home/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 82, in sql_create_model
db_params = f.db_parameters(connection=self.connection)
File "/home/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 548, in db_parameters
type_string = self.db_type(connection)
File "/home/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/jsonfield/fields.py", line 73, in db_type
db_type = cache.get(cache_key)
File "/home/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/django/core/cache/backends/db.py", line 65, in get
"WHERE cache_key = %%s" % table, [key])
File "/home/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/naveen/ENV/automaticfleet/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/naveen/ENV/automaticfleet/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "accounts_cache" does not exist
Related
(Using the library django-tenants for tenant separated multi-tenancy) For PostGis support the docs say to add ORIGINAL_BACKEND = "django.contrib.gis.db.backends.postgis". I have this, however, when I go to create a new tenant I get the following error:
Traceback (most recent call last):
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\celery\app\trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\celery\app\trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
File "C:\Users\Cole\Documents\GitHub\Elevate-RA-Django-App\returns_app\apps\tenant_stores\tasks.py", line 28, in create_tenant_task
tenant.save()
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django_tenants\models.py", line 93, in save
self.create_schema(check_if_exists=True, verbosity=verbosity)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django_tenants\models.py", line 143, in create_schema
verbosity=verbosity)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\core\management\__init__.py", line 141, in call_command
return command.execute(*args, **defaults)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django_tenants\management\commands\migrate_schemas.py", line 63, in handle
executor.run_migrations(tenants=tenants)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django_tenants\migration_executors\standard.py", line 15, in run_migrations
run_migrations(self.args, self.options, self.codename, schema_name, idx=idx, count=len(tenants))
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django_tenants\migration_executors\base.py", line 34, in run_migrations
MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\core\management\commands\migrate.py", line 77, in handle
connection.prepare_database()
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py", line 26, in prepare_database
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "c:\users\cole\appdata\local\programs\python\python36-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "spatial_ref_sys" does not exist
The spatial_ref_sys table exists within my public schema. The django.contrib.gis app is in my shared apps.
Any ideas?
The issue seems to be cause by the default PostGis backend, specifically the call to prepare the database for migration, by explicitly setting the search path prior to calling CREATE EXTENSION IF NOT EXISTS postgis I was able to migrate/create a schema by creating a custom DB backend that overrides this behaviour:
from django.contrib.gis.db.backends.postgis.base import (
DatabaseWrapper as OriginalPostGisDatabaseWrapper,
)
from django_tenants.utils import get_public_schema_name
class DatabaseWrapper(OriginalPostGisDatabaseWrapper):
"""
This database wrapper explicitly sets the search path when preparing the database, as
multi-schema environments (like with Django-tenants) can cause issues with the PostGis
backend.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.PUBLIC_SCHEMA_NAME = get_public_schema_name()
def prepare_database(self):
# Check that postgis extension is installed.
with self.cursor() as cursor:
cursor.execute('SET search_path = %s', params=[self.PUBLIC_SCHEMA_NAME])
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
Then, set your ORIGINAL_BACKEND setting to the location of the above DB backend instead of the standard PostGis backend.
I want to get all Codes Model objects and count the length of the objects returned. I'm getting an error that I can't understand.
This is my model
class Codes(models.Model):
username = models.CharField(max_length=100)
code = models.CharField(max_length=10000)
language = models.CharField(max_length=100)
def __str__(self):
return self.language
I'm using the below function to get all objects from the model
def get_all_codes(lang):
queryset = Codes.objects.filter(language = lang)
return queryset
Calling the function
all_codes_of_lang = get_all_codes(lang)
and trying to count the length using:
if len(all_codes_of_lang) > 0:
print("abc")
leading to this error:
if len(all_codes_of_lang) > 0:
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 240, in __len__
self._fetch_all()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
ProgrammingError: relation "submissions_codes" does not exist
LINE 1: ...odes"."code", "submissions_codes"."language" FROM "submissio...
^
-->
This problem is not with the get_all_codes function or with counting the length, but with a synchronization problem between your models and your database.
Run python manage.py makemigrations and python manage.py migrate to fix.
The models doesn't migrated that's the reason you got this error.
Run following commands
python manage.py makemigrations
python manage.py migrate
I am new to django and trying to complete django following the tutorial. However, I cannot proceed to the next guide. As I type Poll.objects.all(). I receive several errors. Full information is provided below.
>>> Poll.objects.all()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
Poll.objects.all()
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 93, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 108, in __len__
self._result_cache.extend(self._iter)
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 317, in iterator
for row in compiler.results_iter():
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 775, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 840, in execute_sql
cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 41, in execute
return self.cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\sqlite3\base.py", line 366, in execute
six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
File "C:\Python26\lib\site-packages\django\db\backends\sqlite3\base.py", line 362, in execute
return Database.Cursor.execute(self, query, params)
DatabaseError: no such column: polls_poll.pub_date
Since the code given in the question is not sufficient enough, I will provide you the two possible faults:
1) The pub_date column was never created
2) The pub_date column was created, but after the table was created first. So, python manage.py syncdb does not add columns to the existing tables.
So, drop the table from SQL and then do syncdb again to solve this problem. If you do not want to drop the table, use south
I am deploying my django project using amazon ec2 and the bitnami-djangostack ami.
While I was developing the app locally I used mysql. But after deploying the project I used the default for bitnami postgresql.
Now I am getting the following error:
django.db.utils.DatabaseError: relation "appName_appName" does not exist
LINE 1: SELECT (1) AS "a" FROM "appName_appName" WHERE "appName_a...
^
I don't know why the database is naming the database twice (appName_appName).
here is the full terminal:
bitnami#dom:/opt/bitnami/projects/Project$ sudo python manage.py syncdb
/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/__init__.py:60: DeprecationWarning: Short names for ENGINE in database configurations are deprecated. Prepend default.ENGINE with 'django.db.backends.'
DeprecationWarning
/opt/bitnami/projects/Project/cookbook/models.py:94: DeprecationWarning: A Field class whose db_type method hasn't been updated to take a `connection` argument.
class JSONField(models.TextField):
Syncing...
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table registration_registrationprofile
Creating table south_migrationhistory
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yesa
Please enter either "yes" or "no": yes
Username (Leave blank to use 'root'): xxxx
E-mail address: xxxxx
Password: xxx
Password (again): xxx
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/opt/bitnami/python/lib/python2.6/site-packages/South-0.7.6-py2.6.egg/south/management/commands/syncdb.py", line 90, in handle_noargs
syncdb.Command().execute(**options)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 109, in handle_noargs
emit_post_sync_signal(created_models, verbosity, interactive, db)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/sql.py", line 190, in emit_post_sync_signal
interactive=interactive, db=db)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/contrib/auth/management/__init__.py", line 70, in create_superuser
call_command("createsuperuser", interactive=True)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/__init__.py", line 166, in call_command
return klass.execute(*args, **defaults)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 134, in handle
User.objects.create_superuser(username, email, password)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/contrib/auth/models.py", line 140, in create_superuser
u = self.create_user(username, email, password)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/contrib/auth/models.py", line 136, in create_user
user.save(using=self._db)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/base.py", line 570, in save_base
created=(not record_exists), raw=raw, using=using)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/opt/bitnami/projects/Project/cookbook/models.py", line 68, in create_cookbook_for_user
if created and not instance.cookbooks.exists():
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/manager.py", line 192, in exists
return self.get_query_set().exists(*args, **kwargs)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/query.py", line 496, in exists
return self.query.has_results(using=self.db)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/sql/query.py", line 424, in has_results
return bool(compiler.execute_sql(SINGLE))
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/opt/bitnami/apps/django/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
return self.cursor.execute(query, args)
django.db.utils.DatabaseError: relation "appName_appName" does not exist
LINE 1: SELECT (1) AS "a" FROM "appName_appName" WHERE "appName_a...
^
thank you,
katie
I just added the appname_appname to my database and it now works fine.
sorry to mislead - my mistake. I still don't understand why the piece of code wasn't being added to the database on syncdb to begin with though.
but all is well now
I am relatively new to django. I have defined my db schema and validated it with no errors (manage.py validate reports 0 errors found).
Yet when I run ./manage.py syncdb
I get the following stack trace:
Creating table demo_foobar_one
Creating table demo_foobar_two
<snip>...</snip>
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 218, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 347, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 103, in handle_noargs
emit_post_sync_signal(created_models, verbosity, interactive, db)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/sql.py", line 185, in emit_post_sync_signal
interactive=interactive, db=db)
File "/usr/local/lib/python2.6/dist-packages/django/dispatch/dispatcher.py", line 162, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/__init__.py", line 28, in create_permissions
defaults={'name': name, 'content_type': ctype})
File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 135, in get_or_create
return self.get_query_set().get_or_create(**kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 373, in get_or_create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 435, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 528, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 1479, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 783, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 727, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 176, in execute
if not self._defer_warnings: self._warning_check()
File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 92, in _warning_check
warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Data truncated for column 'name' at row 1
I have checked (and double checked) my table schema. All name field are CharField type with maximum length = 64. The backend db I am using is MySQL, so I am sure that indexes can be created for strings of length 64.
What could be causing this error (I suspect it is a misleading error message - even though its coming from the db itself)...
The traceback is happening during the creation of a django.contrib.auth.Permission: some of these get created for your models automatically as part of syncdb.
Permission.name has max_length=50, so you probably have an application and/or model class with a really long name?
Try the following query in manage.py dbshell:
SELECT * FROM auth_permission WHERE LENGTH(name) = 50;
If you cannot change your model name, then you can fix this problem by reducing the length of the generated Permission.name by specifying verbose_name in the model Meta class (see here for more details):
class MyVeryLongModelNameThatIsBreakingMyMigration(models.Model):
class Meta:
verbose_name = 'my long model'
Update
There's an open (as of 2013) Django ticket to fix this:
#8162 (Increase Permission.name max_length)
As Piet Delport noted, the problem is that your model name is too long.
You're certainly going to have to shorten your model name, and then clean up your database. How you do that depends upon where you are in the development process.
If this is a brand new application, with a dedicated database, and no actual data, the simple answer is to drop and recreate the database, and then re-run python manage.py syncdb.
If you have other tables in the database that need to be left alone, but the tables for your Django have no 'real' data, and can thus be dropped without damage, then you can use manage.py sqlclear to generate SQL DDL to drop all of the Django-generated tables, constraints, and indexes.
Do the following:
apps="auth contenttypes sessions sites messages admin <myapp1> <myapp2>"
python manage.py sqlclear ${apps} > clear.sql
You can feed the generated script to mysql or python manage.py dbshell. Once that's done, you can re-run python manage.py syncdb.
If you have actual data in your database tables that can't be dropped or deleted: Slap yourself and repeat 100 times "I will never do development against a production database again. I will always back up my databases before changing them." Now you're going to have to hand-code SQL to change the affected table name, as well as anything else that references it and any references in the auth_permissions table and any other Django system tables. Your actual steps will depend entirely upon the state of your database and tables.
I also got error like this one using postgresql django 1.2, but the problem was not the length, but using ugettext_lazy for translating the names. ('can_purge', _("Can purge")) is evidently unacceptable, since the name is stored in the database as text