Johanna
Johanna

Reputation: 1353

Django - OperationalError: (1054, "Unknown column 'xx' in 'field list'")

I have a problem with a simple insert in my database (as explained in this post). I'm asking a new question as the issue is a bit different.

My model and sql table are the same, I try to insert a row with one field specified and it returns an error saying that it doesn't know another field ..

My sql table looks like this :

FeuilleTemps | CREATE TABLE `FeuilleTemps` (
  `f_id` int(11) NOT NULL AUTO_INCREMENT,
  `f_id_user` int(11) DEFAULT NULL,
  `f_date` date DEFAULT NULL,
  `f_id_proj` int(11) DEFAULT NULL,
  `f_comment` longtext,
  `f_id_task` int(11) DEFAULT NULL,
  `f_unite` int(11) DEFAULT NULL,
  `f_absent` int(11) DEFAULT NULL,
  PRIMARY KEY (`f_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |

Then i called inspectdb --database='myDb' > models.py

And in models.py I have this :

class Feuilletemps(models.Model):
    f_id = models.IntegerField(primary_key=True)
    f_id_user = models.IntegerField(null=True, blank=True)
    f_date = models.DateField(null=True, blank=True)
    f_id_proj = models.IntegerField(null=True, blank=True)
    f_comment = models.TextField(blank=True)
    f_id_task = models.IntegerField(null=True, blank=True)
    f_unite = models.IntegerField(null=True, blank=True)
    f_absent = models.IntegerField(null=True, blank=True)
    class Meta:
        db_table = u'FeuilleTemps'

Then I try to make the following insert query :

f = Feuilletemps(f_id_user=1085)
f.save()

And it gives me this error :

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/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 "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 1436, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1054, "Unknown column 'f_id_proj' in 'field list'")

Upvotes: 0

Views: 6400

Answers (1)

Johanna
Johanna

Reputation: 1353

Ok, I finally figured it out. I needed to call

f.save(using='myDb')

This change in the syntax is a bit confusing as I normally use something like

Feuilletemps.objects.using('myDb').get(f_id=1)

Upvotes: 1

Related Questions