mysql exception when synchronizing db with django's manage.py script - django

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

Related

django loaddata fails because of concurrent database query

I am trying to migrate the database of one my django projects from sqlite to mysql. I first dumped the whole thing with ./manage.py dumpdata > dump.json and prepared the database with ./manage.py migrate and deleted any created data in the tables (This was necessary, as the dump holds all the data).
When I wanted to import the data into the new database with ./manage.py loaddata, there were a lot of errors I was able to resolve, but I can't find the source of this error:
Processed 330984 object(s).Traceback (most recent call last):
File "/app/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 69, in handle
self.loaddata(fixture_labels)
File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 109, in loaddata
self.load_label(fixture_label)
File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 175, in load_label
obj.save(using=self.using)
File "/usr/local/lib/python3.5/site-packages/django/core/serializers/base.py", line 205, in save
models.Model.save_base(self.object, using=using, raw=True, **kwargs)
File "/usr/local/lib/python3.5/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 "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 904, in _save_table
forced_update)
File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 954, in _do_update
return filtered._update(values) > 0
File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py", line 664, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1199, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 894, in execute_sql
raise original_exception
File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 884, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 60, in execute
self.db.validate_no_broken_transaction()
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 448, in validate_no_broken_transaction
"An error occurred in the current transaction. You can't "
django.db.transaction.TransactionManagementError: Problem installing fixture '/path/to/django/dump.json': Could not load auth.User(pk=1): An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
I already tried to remove all signal receivers, so that none of my own code is run when executing loaddata.
Has anyone else experienced similar behaviour with django's loaddata and managed to get it working?
Context:
python v3.5
django v1.11.4
only stdlib django model fields used
This is more of a workaround than a solution, but it did solve my problem.
After further investigation I realized that the auth.user model somehow triggered the query and broke the import.
The solution was to write a simple script to split the dump into multiple parts (I did not have access to the database anymore) and then import them in the correct order: contenttypes, auth, and the rest.
You can find the script here.
This is how I split up the dump:
./investigate-django-dump.py dump.json extract contenttypes. > contenttypes.json
./investigate-django-dump.py dump.json extract auth. > auth.json
./investigate-django-dump.py dump.json extractnot auth.,contenttypes. > data.json

Adding a column in South gives error

I added a couple of columns and fields to my Django model and was able to run South commands such as:
$ python manage.py schemamigration [model_name] --auto
$ python manage.py migrate
fine on local machine without any error. However, when I switch to the server, pulled all new migration+code files and stuff there and did this:
$ python manage.py migrate
It gives me the following error pasted below. I use version MySQL 5.6 (with InnoDB), if that is going to be a useful info. I've tried looking around online and Stackoverflow to find out how to solve this (e.g., this post comes pretty close to helping me understand the issue, but is not exactly what I'm facing...), but I cannot seem to find the solution to this.
What should I try now to successfully proceed the migration?
[root#server: t4s (develop)]$ python manage.py migrate
Running migrations for accounts:
- Migrating forwards to 0012_auto__add_timelimit.
> accounts:0005_auto__add_twilioaccount
> accounts:0006_auto__add_field_twilioaccount_created_at__add_field_twilioaccount_crea
> accounts:0007_auto__del_twilioaccount
> accounts:0008_auto__add_twilioaccount
> accounts:0009_auto__chg_field_twilioaccount_created_by__add_unique_twilioaccount_cre
> accounts:0010_auto__del_twilioaccount
> accounts:0011_auto__add_twilioaccount
> accounts:0012_auto__add_timelimit
- Loading initial data for accounts.
Installed 0 object(s) from 0 fixture(s)
Running migrations for smsmessages:
- Nothing to migrate.
- Loading initial data for smsmessages.
Installed 0 object(s) from 0 fixture(s)
Running migrations for organizations:
- Nothing to migrate.
- Loading initial data for organizations.
Installed 0 object(s) from 0 fixture(s)
Running migrations for campaigns:
- Migrating forwards to 0003_auto__add_field_taskqueue_twilio.
> campaigns:0003_auto__add_field_taskqueue_twilio
! Error found during real run of migration! Aborting.
! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.
! You *might* be able to recover with: - no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `campaigns_taskqueue` DROP COLUMN `twilio_id` CASCADE; []
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: campaigns:0003_auto__add_field_taskqueue_twilio
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.6/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/python2.6/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/usr/lib64/python2.6/site-packages/south/management/commands/migrate.py", line 111, in handle
ignore_ghosts = ignore_ghosts,
File "/usr/lib64/python2.6/site-packages/south/migration/__init__.py", line 220, in migrate_app
success = migrator.migrate_many(target, workplan, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 254, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 329, in migrate_many
result = self.migrate(migration, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 133, in migrate
result = self.run(migration, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 114, in run
return self.run_migration(migration, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 8
migration_function()
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 6
return (lambda: direction(orm))
File "/opt/dev/t4sfeature/t4sdev/t4s/campaigns/migrations/0003_auto__add_field
keep_default=False)
File "/usr/lib64/python2.6/site-packages/south/db/generic.py", line 47, in _ca
return func(self, table, *args, **opts)
File "/usr/lib64/python2.6/site-packages/south/db/generic.py", line 418, in ad
self.execute(sql)
File "/usr/lib64/python2.6/site-packages/south/db/generic.py", line 282, in ex
cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 69, i
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 53, i
return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line
return self.cursor.execute(query, args)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 175, in exe
if not self._defer_warnings: self._warning_check()
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 89, in _war
warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: TIME/TIMESTAMP/DATETIME columns of old format have been upgraded
When I tried running python manage.py migrate again, I got a slightly different error as follows.
[root#server: t4s (develop)]$ python manage.py migrate
Running migrations for accounts:
- Nothing to migrate.
- Loading initial data for accounts.
Installed 0 object(s) from 0 fixture(s)
Running migrations for smsmessages:
- Nothing to migrate.
- Loading initial data for smsmessages.
Installed 0 object(s) from 0 fixture(s)
Running migrations for organizations:
- Nothing to migrate.
- Loading initial data for organizations.
Installed 0 object(s) from 0 fixture(s)
Running migrations for campaigns:
- Migrating forwards to 0003_auto__add_field_taskqueue_twilio.
> campaigns:0003_auto__add_field_taskqueue_twilio
FATAL ERROR - The following SQL query failed: ALTER TABLE `campaigns_taskqueue` ADD COLUMN `twilio_id` integer NULL;
The error was: (1060, "Duplicate column name 'twilio_id'")
! Error found during real run of migration! Aborting.
! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.
! You *might* be able to recover with: - no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `campaigns_taskqueue` DROP COLUMN `twilio_id` CASCADE; []
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: campaigns:0003_auto__add_field_taskqueue_twilio
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.6/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/python2.6/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/usr/lib64/python2.6/site-packages/south/management/commands/migrate.py", line 111, in handle
ignore_ghosts = ignore_ghosts,
File "/usr/lib64/python2.6/site-packages/south/migration/__init__.py", line 220, in migrate_app
success = migrator.migrate_many(target, workplan, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 254, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 329, in migrate_many
result = self.migrate(migration, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 133, in migrate
result = self.run(migration, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 114, in run
return self.run_migration(migration, database)
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 84, in run_migration
migration_function()
File "/usr/lib64/python2.6/site-packages/south/migration/migrators.py", line 60, in <lambda>
return (lambda: direction(orm))
File "/opt/dev/t4sfeature/t4sdev/t4s/campaigns/migrations/0003_auto__add_field
keep_default=False)
File "/usr/lib64/python2.6/site-packages/south/db/generic.py", line 47, in _ca
return func(self, table, *args, **opts)
File "/usr/lib64/python2.6/site-packages/south/db/generic.py", line 418, in ad
self.execute(sql)
File "/usr/lib64/python2.6/site-packages/south/db/generic.py", line 282, in ex
cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 69, i
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 53, i
return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/utils.py", line 99, in __exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 53, i
return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line
return self.cursor.execute(query, args)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 173, in exe
self.errorhandler(self, exc, value)
File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in
raise errorclass, errorvalue
django.db.utils.OperationalError: (1060, "Duplicate column name 'twilio_id'")
The lastest south migration file (named 0003_auto__add_field_taskqueue_twilio.py) regarding campaigns models has this:
u'campaigns.taskqueue': {
'Meta': {'object_name': 'TaskQueue'},
'campaign': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['campaigns.Campaign']"}),
'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'created_by': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['organizations.Group']", 'symmetrical': 'False'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'launch_time': ('django.db.models.fields.DateTimeField', [], {}),
'status': ('django.db.models.fields.CharField', [], {'default': "'pending'", 'max_length': '30'}),
'twilio': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['accounts.TwilioAccount']", 'null': 'True'})
},
As far as I remember, I just added this line to the campaigns/models.py file:
class TaskQueue(models.Model):
campaign = models.ForeignKey(Campaign)
launch_time = models.DateTimeField()
twilio = models.ForeignKey(TwilioAccount, null=True) # new line recently added
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User)
and TwilioAccount model is like this:
class TwilioAccount(models.Model):
name = models.CharField(max_length=50, blank=True)
number = models.CharField(max_length=16, blank=True)
sid = models.CharField(max_length=100, blank=True)
token = models.CharField(max_length=100, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
Thank you very much for your answers.
I found a solution to this. I read through South's migration file and git commit logs as well as checked my DB tables and realized there's a column named twilio already created (not sure when and how). After ensuring everything looks good, I used the recommendation of this very useful post (provided in the question above) by doing:
$ python manage.py migrate 0003 --fake
That allowed me to skip the migration #3 (I don't need it because I already have twilio column created in my DB) and proceed to next steps in coding. I hope someone who face the same error will find this useful.

I can't save data to database with Django and SQL Anywhere 11

I have connected django with sql anywhere 11 database, I have follow the instructions on the page SQL Anywhere Django Driver from github.
I have created the database with this command:
C:\>dbinit -z UCA django.db --> (database created successful)
I have started the Database Server with command:
C:\>dbsrv11 django.db --> (database server started successful)
I have created my project with:
C:\>django-admin.py startproject mysite (at this point all it's ok)
I have edited the file mysite/mysite/settings.py and I have changed the DATABASES setting with this:
DATABASES = {
'default': {
'ENGINE': 'sqlany_django',
'NAME': 'django',
'USER': 'dba',
'PASSWORD': 'sql',
'OPTIONS': {'eng': 'django'}
}
}
When I want to sync database using this command:
c:/mysite>python manage.py syncdb
the prompt show me a error message (the error message is shown below)
I have connected Sybase Central to django.db I see the tables was created.
but when I try save data to the tables this data wasn't recorded
even when I have created the superuser with this command:
c:\mysite>python manage.py createsuperuser --username=joe --email=joe#example.com
Password:123
Password (again):123
Superuser created successfully.
I querying the datebase with:
select * from auth_user
No rows are shown
The data aren't saved
I have an environment for test with this features:
O.S.: Windows XP
DB Engine: SQL Anywhere 11 with EBF 3069
Python 2.7
Django 1.6.1
Setuptools installed
PIP installed
sqlanydb installed
sqlany-django installed
Here is the error message
C:\mysite>python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table Personal_persona
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
399, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 242,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 285,
in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 415,
in handle
return self.handle_noargs(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py"
, line 112, in handle_noargs
emit_post_sync_signal(created_models, verbosity, interactive, db)
File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 216,
in emit_post_sync_signal
interactive=interactive, db=db)
File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py", line 185,
in send
response = receiver(signal=self, sender=sender, **named)
File "C:\Python27\lib\site-packages\django\contrib\auth\management\__init__.py
", line 101, in create_permissions
auth_app.Permission.objects.using(db).bulk_create(perms)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 356, in b
ulk_create
self._batched_insert(objs_without_pk, fields, batch_size)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 835, in _
batched_insert
using=self.db)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 232, in
_insert
return insert_query(self.model, objs, fields, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1511, in
insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 90
3, in execute_sql
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 69, in e
xecute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 53, in e
xecute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 53, in e
xecute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\sqlany_django\base.py", line 87, in execut
e
ret = self.cursor.execute(trace(query), trace(args))
File "C:\Python27\lib\site-packages\sqlanydb.py", line 714, in execute
self.executemany(operation, [parameters])
File "C:\Python27\lib\site-packages\sqlanydb.py", line 685, in executemany
bind_count = self.api.sqlany_num_params(self.stmt)
File "C:\Python27\lib\site-packages\sqlanydb.py", line 619, in __stmt_get
self.handleerror(*self.parent.error())
File "C:\Python27\lib\site-packages\sqlanydb.py", line 613, in handleerror
eh(self.parent, self, errorclass, errorvalue)
File "C:\Python27\lib\site-packages\sqlanydb.py", line 342, in standardErrorHa
ndler
raise errorclass(errorvalue)
django.db.utils.OperationalError: Syntax error near ',' on line 1
Sorry, but the SQL Anywhere Django driver requires SQL Anywhere v12 or higher; you cannot use it with SQL Anywhere v11. I have confirmed that the driver uses features of the database server that were not supported in version 11.
Disclaimer: I work for SAP in SQL Anywhere engineering.

Phantom ForeignKey to similar model representing a DB view

I'm trying to get this 'phantom' ForeignKey to reference a model that is a view on the backend. The view assembles data that includes some data from the originating table(so they share a primary key).
Here's a simplified version of what we've had to setup(view is more complex than just the model, so it was chosen to design it this way).
class VSpouse(models.Model):
person_id = models.IntegerField(primary_key=True)
...
class Person(models.Model):
person_id = models.AutoField(primary_key=True)
spouse = models.ForeignKey(VSpouse, db_column = 'person_id', to_field='person_id', null=True)
...
Now, since the tables were designed on the backend before models, we have never used syncdb. Because of that, we've never noticed a problem and things work just as expected.
However, now we're starting to develop some Django tests, and when it begins to build the test DB, we see the following:
$ python2 manage.py test misc --traceback
Creating test database for alias 'default'...
Destroying old test database 'default'...
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 222, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/test.py", line 72, in execute
super(Command, self).execute(*args, **options)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/test.py", line 89, in handle
failures = test_runner.run_tests(test_labels)
File "/usr/lib/python2.7/site-packages/django/test/simple.py", line 367, in run_tests
old_config = self.setup_databases()
File "/usr/lib/python2.7/site-packages/django/test/simple.py", line 315, in setup_databases
self.verbosity, autoclobber=not self.interactive)
File "/usr/lib/python2.7/site-packages/django/db/backends/creation.py", line 293, in create_test_db
load_initial_data=False)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 161, in call_command
return klass.execute(*args, **defaults)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute
output = self.handle(*args, **options)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 385, in handle
return self.handle_noargs(**options)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 102, in handle_noargs
cursor.execute(statement)
File "/usr/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 58, in execute
six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute
return self.cursor.execute(query, args)
DatabaseError: column "person_id" specified more than once
Now, I guess my question is, is there any 'proper' ways of doing this or working around this problem? It would be great if I could just add a switch like real=False to the field definition so it wouldn't try and generate the column when building the test DB. However, I don't think that's currently possible.
Actually, you can. Only it's not called real, but abstract, and it applies to models (but you should be able to use inheritance to address this).
Your model structure might need a little massaging to correspond to what Django expects, but that should get you started in the right direction.
It's documented here.

Relational DatabaseError in django postgresql database after deploying project to bitnami

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