Django query returns nothing for legacy databse

36 views
Skip to first unread message

Galil

unread,
Jun 9, 2016, 12:13:22 PM6/9/16
to Django users
Hello,

I am using two databses for my Django app and one of them is an existing MySQL database: 

DATABASES = {
 
'default': {
 
'ENGINE': 'django.db.backends.sqlite3',
 
'NAME': os.path.join(BASE_DIR, 'users.sqlite3'),
 
},
 
'calls': {
 
'ENGINE': 'django.db.backends.mysql',
 
'NAME': 'portal2',
 
'USER': 'cdraccess',
 
'PASSWORD': '******',
 
'HOST': '[MY_HOST]',
 
},

}

I used inspectdb to create the models for the call and they look like:
class Calls(models.Model):
 callid
= models.AutoField(db_column='CallID', primary_key=True) # Field name made lowercase.
 conferencename
= models.CharField(db_column='ConferenceName', max_length=200) # Field name made lowercase.
 jointime
= models.DateTimeField(db_column='JoinTime') # Field name made lowercase.
 leavetime
= models.DateTimeField(db_column='LeaveTime', blank=True, null=True) # Field name made lowercase.

 
class Meta:
 managed
= True
 db_table
= 'ConferenceCall2'


I called makemigrations and then migrate and everything looks fine.

However, when I run a query like Calls.objects.all() the result is: []. 

Or when I tried Calls..objects.count() the result was: 0. The MySQL database is not empty, but apparently Django ORM believes that it is.

Am I missing something?

Gagaro

unread,
Jun 9, 2016, 12:34:19 PM6/9/16
to Django users
Hi,

You should select the database you want to use: Calls.objects.using('calls').all()

Remco Gerlich

unread,
Jun 9, 2016, 12:43:04 PM6/9/16
to django...@googlegroups.com
Do you have a database router set up to tell Django which models are stored in which database?

Greetings,
Remco Gerlich

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c7052a27-e3ac-41fa-90a1-a1c266cd1c71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Galil

unread,
Jun 10, 2016, 11:07:11 AM6/10/16
to Django users
@Gagaro, thanks! That worked for me!

@Remco Gerlich, no I don't have a database router as what I am doing now is very simple But thank you for you answer anyway!
Reply all
Reply to author
Forward
0 new messages