Unknown column '' in 'field list'. Django - python

I'm using Django 1.9 and MySQL. I want to rename a field in my model. Let's look at model Choice from the Django getting started tutorial.
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length = 200)
votes = models.IntegerField(default=0)
So, I want to rename the votes field to votes_count. I created an empty migration and add to operations following line:
migrations.RenameField (
model_name='choice',
old_name='votes',
new_name='votes_count',
),
After python manage.py migrate, the field in the database table was renamed. But when I tried to use this model
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id);
try:
selected = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {
'question':question,
'error_message':"You didn't select a choice."
})
else:
selected.votes_count += 1
selected.save()
return HttpResponseRedirect(reverse('polls:results', args=(question_id)))
I got:
Unknown column 'polls_choice.votes' in 'field list
Traceback:
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
112. return self.cursor.execute(query, args)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
226. self.errorhandler(self, exc, value)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
36. raise errorvalue
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
217. res = self._query(query)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_query
378. rowcount = self._do_query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_do_query
341. db.query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in query
280. _mysql.connection.query(self, query)
The above exception ((1054, "Unknown column 'polls_choice.votes' in 'field list'")) was the direct cause of the following exception:
File "/home/verdigo/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./polls/views.py" in vote
23. selected = question.choice_set.get(pk=request.POST['choice'])
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in get
381. num = len(clone)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in __len__
240. self._fetch_all()
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
52. results = compiler.execute_sql()
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
848. cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/utils.py" in
__exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/utils/six.py" in reraise
685. raise value.with_traceback(tb)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/verdigo/venv/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
112. return self.cursor.execute(query, args)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
226. self.errorhandler(self, exc, value)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
36. raise errorvalue
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
217. res = self._query(query)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_query
378. rowcount = self._do_query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/cursors.py" in
_do_query
341. db.query(q)
File "/home/verdigo/venv/lib/python3.4/site-packages/MySQLdb/connections.py" in query
280. _mysql.connection.query(self, query)
Exception Type: OperationalError at /polls/1/vote/
Exception Value: (1054, "Unknown column 'polls_choice.votes' in 'field list'")

It looks as if you created and ran a migration to rename the model field from votes to votes_count, but did not update the model at the same time.
When the Django tries to fetch the model from the db, it tries to select the votes column because you still have a votes field in your models, and you get the error because the column doesn't exist in the database.
Creating a manual migration isn't normally necessary. Usually, you would rename the model field, run makemigrations, then run migrate. The advantage of letting Django create the migration is that you can be confident that the database is in sync with your models after you have run migrate.

Cause: This error occurs when a new one-to-many foreign key is created, pointing to the model with the forkey field and then generating the table
.
Solution: This problem can be solved by directly deleting the database and rebuilding it.
1: drop database database table;
2, re-create after deletion, before the code created, run directly

This is an issue that has persisted with me and lead me down a lot of rabbit holes dropping tables etc. A simple solution I have found is answering "N" when django asks you if you are renaming a field of that model (when running makemigrations). What that then essentially performs is a deletion of your previous field and creates the new field. Be careful as you may lose data on existing field so this works with fields which are either new or relatively easy to 'refill' their data required. You may need to run --fake if you get an error with regards to not being able to 'drop field' when migrating after makemigrations.
Update:
I did the above for a Boolean field and my data was kept. Even though I said N, it seems as if it is essentially a renaming.

Related

How to access tables from a different schema in oracle 11g using django?

I have a user named mi_abc in oracle 11g.
The user do not have any table in the database but has access to all the tables in another schema sch_abc.
When I run a normal select query from sqldeveloper on the sch_abc schema from mi_abc, it works perfectly fine, but when I use django, I am always getting the error:-
django.db.utils.DatabaseError: ORA-00942: table or view does not exist
I tried to set the db_table = sch_abc.tableName and also set db_table = tableName but both gives me the same error. Any clue how to resolve this?
TRACE:-
Traceback (most recent call last):
File "C:\xxx\xxx\xxx\xxx\xxx\xxx\xxxx\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\xxx\xxx\xxxx\xxxx\xxxx\Python\Python37\lib\site-packages\django\utils\deprecation.py", line 142, in __call__
response = self.process_response(request, response)
File "C:\xxxx\xxxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\contrib\sessions\middleware.py", line 58, in process_response
request.session.save()
File "C:\xxx\xxxx\xxxx\xxxx\xxxxx\Python\Python37\lib\site-packages\django\contrib\sessions\backends\db.py", line 81, in save
return self.create()
File "C:\xxxx\xxxxx\xxxx\xxxx\xxxxx\Python\Python37\lib\site-packages\django\contrib\sessions\backends\db.py", line 50, in create
self._session_key = self._get_new_session_key()
File "C:\xxxx\xxxxx\xxxxx\xxxxx\xxxx\Python\Python37\lib\site-packages\django\contrib\sessions\backends\base.py", line 164, in _get_new_session_key
if not self.exists(session_key):
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\contrib\sessions\backends\db.py", line 46, in exists
return self.model.objects.filter(session_key=session_key).exists()
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\models\query.py", line 673, in exists
return self.query.has_results(using=self.db)
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\models\sql\query.py", line 517, in has_results
return compiler.has_results()
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 858, in has_results
return bool(self.execute_sql(SINGLE))
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 899, in execute_sql
raise original_exception
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 889, in execute_sql
cursor.execute(sql, params)
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\xxx\xxx\xxx\xxx\xxx\Python\Python37\lib\site-packages\django\db\backends\oracle\base.py", line 497, in execute
return self.cursor.execute(query, self._param_generator(params))
django.db.utils.DatabaseError: ORA-00942: table or view does not exist
Well, I resolved the issue and let me tell you there is no straight way to do it in django. The problem with my application was, I was using the authetication features of django and also session handling. All these tables are created by django directly on the initial migration. So, there is no existence of them in the models.py file that you can simply append the schema name and ask your application to connect to the table of that schema.
What I ended up doing is, I created private synonyms to all the tables of the other schema which actually contained those tables. If you do this, you don't have to change anything in your django code. Your application will simply work because oracle will do the dirty work of actually connecting to the proper table. You will merely call the table in your application as if its your own. In this way when django looks for tables like django_session, auth_user etc, it simply queries it like it always does and oracle redirects it to the actual tables present in another schema.
Hope this helps people who face this issue in the future.
This is by no means officially supported, but this works in Postgres:
class Meta:
managed = False
db_table = 'schema\".\"table'
It took some trial and error for Postgres, but you can probably do something similar for Oracle. This is because the Postgres engine quotes object names, and this fakes the quoting mechanism out.
UPDATE:
After doing some digging, I found this for Oracle (modified for Python 3):
class Meta:
db_table = '"SCHEMA"."TABLE_NAME"'
Source: https://code.djangoproject.com/ticket/14136
I would recommend keeping managed = False unless you really know what you're doing. Good luck!
You can set the required schema, before executing the command. and then go back to public schema once the queryset is processed.
from django.db import connection
connection.set_schema(schema_name)

Django Foreign Key of Auth.User - column "user_id" does not exist

UPDATE:
This is probably a better representation of my question:
Using loadtestdata, how do you populate the auth.User database? I just want to populate the database with bogus users, and simulations that are linked to those bogus users.
I have looked at all relevant resources but I'm unable to make any headway.
Situation:
I am building a simulation model using Django and am looking to store simulation data as well as sets of parameter data. Many sets of simulation data should be linked to each user, and many sets of parameter data can be linked to each simulation. Thus, I have tried to model this under 'models.py' of my Django app.
from django.db import models
from django.conf import settings
# Create your models here.
class Simulation(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, )
date = models.DateTimeField()
# Each simulation has only one graph
# Graphing parameters
hill_num = models.CharField(max_length=3)
div_type = models.CharField(max_length=10)
s3_url = models.CharField(max_length=256)
def __str__(self):
return str(self.sim_id)
class Parameter(models.Model):
# Each simulation can have many sets of simulation parameters
simulation = models.ForeignKey('Simulation', on_delete=models.CASCADE)
lsp = models.PositiveIntegerField()
plots = models.PositiveIntegerField()
pioneer = models.BooleanField()
neutral = models.BooleanField()
# for pioneers
p_max = models.PositiveIntegerField()
p_num = models.PositiveIntegerField()
p_start = models.PositiveIntegerField()
# for non-pioneers
np_max = models.PositiveIntegerField()
np_num = models.PositiveIntegerField()
np_start = models.PositiveIntegerField()
def __str__(self):
return str(self.param_id)
./manage.py makemigrations works but when I try to populate the database with python manage.py loadtestdata auth.User:10 divexplorer.Simulation:40 divexplorer.Parameter:300, it throws this error:
auth.User(pk=72): JshtkqSzw3
auth.User(pk=73): QwPfxJc_KS1k5sgH5BN98J
auth.User(pk=74): fuEhnZ
auth.User(pk=75): a
auth.User(pk=76): XjVXXLYGz3MJSfmZ54wGxXo
auth.User(pk=77): fhOWIp
auth.User(pk=78): 5tkEhKOjX2UUbFe
auth.User(pk=79): JgG4Y4PqkcapNJJOlFW1LOQ
auth.User(pk=80): fhRmfQHNim4zM8hGPzpYdkwaHI7
auth.User(pk=81): cEPQtyByKdUs8Gw58DrfNtpsCRB_
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/management/commands/loadtestdata.py", line 225, in handle
autofixture.create(model, count, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/__init__.py", line 136, in create
return autofixture.create(count, **create_kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/base.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/autofixture/base.py", line 519, in create_one
instance.save()
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/Users/evanma/anaconda/lib/python2.7/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 "/Users/evanma/anaconda/lib/python2.7/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 "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/evanma/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "user_id" of relation "divexplorer_simulation" does not exist
LINE 1: INSERT INTO "divexplorer_simulation" ("user_id", "date", "hi...
I have spent hours trying to work through this error but to no avail. Any ideas?
I have tried renaming the argument db_column in the ForeignKey function, applying default values, but none of them work. Would appreciate some input thank you very much!
I believe you need to specify if related models should also be created with random values.
Please check the docs: http://django-autofixture.readthedocs.io/en/latest/loadtestdata.html
Where it is stated:
There are a few command line options available. Mainly to control the behavior of related fields. If foreingkey or many to many fields should be populated with existing data or if the related models are also generated on the fly. Please have a look at the help page of the command for more information:
django-admin.py help loadtestdata
Unfortunately I can't check a running instance of Django know in order to point you to the exact option and it's value but checking the docs I would say that you have to use this option from loadtestdata:
...
make_option('--generate-fk', action='store', dest='generate_fk', default=None,
help=u'Do not use already existing instances for ForeignKey relations. ' +
'Create new instances instead. You can specify a comma sperated list of ' +
'field names or ALL to indicate that all foreignkeys should be generated ' +
'automatically.'),
...

Django unittests fail because of fixture load issue

I use fixtures for prepopulating some simple auxilary data.
I can successfully load fixtures using manage.py loaddata fixtures/initial_data.json.
A problem appears when I try to run a unit test. It gives me the following error:
File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 600, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 549, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: Problem installing fixture '/opt/prjoect/myapp/fixtures/initial_data.json': Could not load myapp.SomeModel(pk=1): relation "myapp_somemodel" does not exist
LINE 1: UPDATE "myapp_somemodel" SET "name" = 'M' WHERE "myapp_somemodel"."id" = 1
Following tables are presented in regular database. This fixture doesn't have foreign keys, only simple tables, with key and other values.
I have only one initial_migration in my migrations module.
Where is the problem? I have no idea what could be the root cause.
I use Django 1.7, python 3.4, Postrgesql, Ubuntu 14.04
It seems your fixture is trying to update an object with id=1:
LINE 1: UPDATE "myapp_somemodel" SET "name" = 'M' WHERE "myapp_somemodel"."id" = 1
and this object does not exist:
Could not load myapp.SomeModel(pk=1)...
Do you have an object of myapp_somemodel1 with id/pk = 1 ?
Maybe you had it when you generate the fixture, and after you've deleted it. You could try to do in the shell:
from myapp.models import somemodel
new_object = Somemodel(id=1, name='anything')
new_object.save()
And then try to load the fixture again

IntegrityError at non-existing field

I'm having troubles with adding new copy of existing model Clients, which looks like:
class Client(models.Model):
user = models.OneToOneField(User) # Extending default user model
organization = models.CharField(max_length=40)
def __unicode__(self):
return self.user.first_name + " " + self.user.last_name
Im entering shell and type this:
from django.contrib.auth.models import User
from mysiteApp.models import Client
user = User.objects.get(pk=2) # User with pk 2 exists
client = Client(user=user, organization="someorg") # copy creates succesfully
But then, im trying to save copy by
client.save()
And im getting this:
>>> client.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 537, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 641, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1559, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 844, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 41, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 389, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 387, in execute
return Database.Cursor.execute(self, query, params)
IntegrityError: mysiteApp_client.cID may not be NULL
The thing is that i HAD such field as cID in Client model before, wich really had NOT NULL, but now i dont
manage.py sql mysiteApp shows:
BEGIN;
CREATE TABLE "mysiteApp_client" (
"id" integer NOT NULL PRIMARY KEY,
"user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id"),
"organization" varchar(40) NOT NULL
)
;
manage.py syncdb changes nothing, what should i do?
Thanks.
Your database is still in the state as where the cID field is required. This is because syncdb does not alter already existing tables.
To overcome your problem you have three options:
a) delete the Client table (=lose your data) in your database and run syncdb again
b) manually modifying your database using a sql ALTER TABLE command
c) use a migration tool like South (introduction) to reflect the changes you did for models.py in your database. I would recommend to learn how to deal with South, as once you are in production you probably need such a migration tool.

IntegrityError: column user_id is not unique in django tastypie test unit

I have an estrange error testing a resourcemodel in tastypie.
I have this test
class LocationResourceTest(ResourceTestCase):
# Use ``fixtures`` & ``urls`` as normal. See Django's ``TestCase``
# documentation for the gory details.
fixtures = ['admin_user.json']
def runTest(self):
super(LocationResourceTest, self).runTest()
def setUp(self):
super(LocationResourceTest, self).setUp()
# Create a user.
self.user = User.objects.create_user(username='johndoe',email='johndoe#example.com',password='password')
# Create an API key for the user:
ApiKey.objects.create(user=self.user)
def get_credentials(self):
return self.create_apikey(username=self.user.username, api_key=self.user.api_key.key)
def test_create_apikey(self):
# Try api key authentication using ResourceTestCase.create_apikey().
credentials = self.get_credentials()
resp = self.api_client.get('/api/v1/location/',authentication=credentials,format='json')
self.assertHttpOK(resp)
def test_get_list_unauthorzied(self):
pass
When I execute the test,I get the following error
======================================================================
ERROR: test_get_list_unauthorzied (geolocation.tests.api.LocationResourceTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/phantomis/Memoria/smartaxi_server/geolocation/tests/api.py", line 24, in setUp
ApiKey.objects.create(user=self.user)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/manager.py", line 137, in create
return self.get_query_set().create(**kwargs)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/query.py", line 377, in create
obj.save(force_insert=True, using=self.db)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django_tastypie-0.9.12_alpha-py2.7.egg/tastypie/models.py", line 47, in save
return super(ApiKey, self).save(*args, **kwargs)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/query.py", line 1593, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 912, in execute_sql
cursor.execute(sql, params)
File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 344, in execute
return Database.Cursor.execute(self, query, params)
IntegrityError: column user_id is not unique
The error is in the method "test_get_list_unauthorzied" , is weird because if i comment that method, my test pass (that method is completely empty)
setUp is run for each of the test_* methods you have. When you comment out the empty test case, setUp is only run once. With the other test_* method not commented out, setUp is run twice, and this is how the uniqueness constraint gets violated.
I would create a tearDown method that deletes the user and api key you created in the other test case.

Categories