3

I'm facing a problem with multiple database with Django 1.11.1.

I always recieve:

OperationalError at /admin/news/article/
no such table: news_article

When I click "Artikel" on the Admin-page.

My project contains two databases, one for recipies, one for a RSS newsfeed. I defined them like this in the settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, '..', 'cookbook.db'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
    'news_db': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, '..', 'news.db'),
    },
}

I also added a router in routers.py:

class CookbookRouter(object):
"""
A router to control all database operations on models in the
cookbook site.
"""
def db_for_read(self, model, **hints):
    if model._meta.app_label == 'news':
        return 'news_db'
    return None

def db_for_write(self, model, **hints):
    if model._meta.app_label == 'news':
        return 'news_db'
    return None

def allow_relation(self, obj1, obj2, **hints):
    if obj1._meta.app_label == 'news' or \
       obj2._meta.app_label == 'news':
       return True
    return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
    if app_label == 'news':
        return db == 'news_db'
    return None

And I set it up like this in settings.py:

DATABASE_ROUTERS = ['cookbook.routers.CookbookRouter']

My news.model.py looks like this:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from basemodels import DateTimeInfo

# Create your models here.


class Article(DateTimeInfo):
    headline = models.CharField(u'Überschrift', max_length=100)
    body = models.TextField(u'Inhalt')

class Meta:
    verbose_name = u'Artikel'
    verbose_name_plural = u'Artikel'
    ordering = ['-date_updated']

def __unicode__(self):
    return self.headline

After running

$python manage.py makemigration 
$python manage.py migrate 

several times and making shure that the Table article shows up if i run $python manage.py inspectdb output:

...
class NewsArticle(models.Model):
    id = models.IntegerField(primary_key=True)  # AutoField?
    date_created = models.DateTimeField()
    date_updated = models.DateTimeField()
    headline = models.CharField(max_length=100)
    body = models.TextField()

    class Meta:
        managed = False
        db_table = 'news_article'
...

What am I doing wrong?

  • Did you create the 'news_article' manually? Is it a legacy table? – yorodm Jun 06 '17 at 13:52
  • How can i check if ist a legacy table? (and what is a legacy table) I thought runnig "makemigrations" and "migrate" would create all tables defined in the models.py while a database router is defined. Now i solved the Problem with calling: $python manage.py migrate --database=news_article – FantaRainer Jun 06 '17 at 14:17
  • By legacy table I mean "a table that already existed and you need to use in your system". `makemigrations` will not create table or delete unmanaged tables so either remove the `managed=false` or create it by hand. – yorodm Jun 06 '17 at 14:22
  • You are using sqlite. That means this project definitely isn't big enough to warrant two different databases. Just use one all your problems will go away – e4c5 Jun 08 '17 at 10:38

0 Answers0