Valchris
Valchris

Reputation: 1481

Django error only on MySQL database

Locally I use sqllite3 as a database, but in production I use MySQL. When I moved my project to production I encountered this error:

  ...
File "S:\Python27\lib\site-packages\django\templatetags\i18n.py", line 46, in
render
    langs = self.languages.resolve(context)
  File "S:\Python27\lib\site-packages\django\template\base.py", line 653, in res
olve
    value = self._resolve_lookup(context)
  File "S:\Python27\lib\site-packages\django\template\base.py", line 692, in _re
solve_lookup
    raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bit, current
)) # missing attribute
VariableDoesNotExist: Failed lookup for key [LANGUAGES] in u'[{}, {}, {}]'

I'm using django's i18n

Any ideas on where this is coming from? Google didn't inform much, also what data would be useful to look at for debugging? I pointed my local test environment to the production DB and got the same error.

My LANGUAGES var in settings.py is:

LANGUAGES = (
    ('en',gettext('English')),
    ('fr',gettext('French')),
)

EDIT With debug turned on I get the error:

Caught OperationalError while rendering: (1242, 'Subquery returns more than 1 row')

on the line {% for f in frames %}

My Frame model is:

@I18n('title','description')
class Collection(models.Model):
    title = models.CharField(max_length=255)
    description = models.CharField(max_length=255)
    order = models.IntegerField(default=100)
    image = models.ImageField(upload_to="collection/images",null=True)
    rollover = models.ImageField(upload_to="collection/images",null=True,blank=True)
    publish = models.BooleanField(default=True)

    def __unicode__(self):
        return self.title

@I18n('title')
class Frame(models.Model):
    title = models.CharField(max_length=255)
    collection = models.ManyToManyField(Collection)
    order = models.IntegerField(default=100)
    image = models.ImageField(upload_to="frame/images")
    rollover = models.ImageField(upload_to="frame/images",null=True,blank=True)
    publish = models.BooleanField(default=True)

    def __unicode__(self):
        return self.title

EDIT #2

The troublesome MySQL lines, that doesn't fail in SQLite

SELECT `FrontEnd_article`.`id`, `FrontEnd_article`.`title_fr`, `FrontEnd_article`
.`title_en`, `FrontEnd_article`.`text_fr`, `FrontEnd_article`.`text_en`, 
`FrontEnd_article`.`date`, `FrontEnd_article`.`image`, `FrontEnd_article`.`image_text`, 
`FrontEnd_article`.`can_comment`, `FrontEnd_article`.`order`, 
`FrontEnd_article`.`publish` FROM `FrontEnd_article` INNER JOIN 
`FrontEnd_article_frames` ON (`FrontEnd_article`.`id` = 
`FrontEnd_article_frames`.`article_id`) WHERE `FrontEnd_article_frames`.`frame_id` =  
(SELECT U0.`id` FROM `FrontEnd_frame` U0 INNER JOIN `FrontEnd_frame_collection` U1 ON 
(U0.`id` = U1.`frame_id`) WHERE (U1.`collection_id` = 1  AND U0.`publish` = True )) 
ORDER BY `FrontEnd_article`.`date` DESC

Thanks again for the help!

Upvotes: 0

Views: 638

Answers (2)

Valchris
Valchris

Reputation: 1481

SELECT * FROM FrontEnd_article INNER JOIN FrontEnd_article_frames ON 
(FrontEnd_article.id = FrontEnd_article_frames.article_id) WHERE 
FrontEnd_article_frames.frame_id = (SELECT U0.id FROM FrontEnd_frame U0 INNER JOIN 
FrontEnd_frame_collection U1 ON (U0.id = U1.frame_id) WHERE (U1.collection_id = 1  AND 
U0.publish = True ))

This is the simplified version of what is generated by Django. When I broke down this query I realised that inner join assumes that only 1 value is returned from:

(SELECT U0.id FROM FrontEnd_frame U0 INNER JOIN FrontEnd_frame_collection U1 ON 
(U0.id = U1.frame_id) WHERE (U1.collection_id = 1  AND U0.publish = True ))

Which in fact returned a result set of data. The fix was simple once I found it, I replaced the "=" with an "IN" and it worked correctly.

FrontEnd_article_frames.frame_id IN (SELECT U0.id FROM FrontEnd_frame U0 INNER JOIN
FrontEnd_frame_collection U1 ON (U0.id = U1.frame_id) WHERE (U1.collection_id = 1  AND 
U0.publish = True ))

Upvotes: 0

Dirk Eschler
Dirk Eschler

Reputation: 2569

Is the traceback the same if you enable DEBUG temporarily? Error messages can be misleading when it is disabled. It could as well be a wrong database setting and Django bails out at a different stage.

Upvotes: 2

Related Questions