Hello,
I wanted to be able to access a table from an external database. I was not sure how. The simple way, that I tried was using connection and cursor to do a query to get the user information from the external database and create a user in the default Django database using this information. I only tried with fetchone, but now I cannot delete that user. When I try to delete the user it give sme the error no such table: UserData (this is the table name from the external database). I am not sure why it would do that considering I feel like I am copying the information from the external table not linking to it. I hope this makes sense.
For us to be able to advise you, you’ll need to show the specific (the models and the code) that are performing these operations, along with providing the complete error message with the traceback that you are receiving.
Side note: When posting code (or templates, error messages, tracebacks, etc), enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.
Okay, thank you. I set up a Database router like below. I wanted to use the users in this external database as the users for the Django database in my project. Although the external database did not have an equivalent User model. I this ran this SQL query and tried to transfer that data. I certainly do not think this is the most sustainable method. I also thought the database router would help me access the data with out having to transfer it, but I had issues with that mostly because I was not sure how to do that.
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'user_db': {
'ENGINE':'django.db.backends.mysql',
'NAME': 'user_datadb',
'USER': 'username',
'PASSWORD': 'password',
'HOST':'183.9.0.1',
'PORT':3333,
}
}
def transfer_data(request):
with connections['user_db'].cursor() as cursor:
cursor.execute("SELECT * FROM UserData")
external_users = cursor.fetchall()
user_dict=[]
i=0
for user in external_users:
user_dict.append(user)
print(user_dict[i][3])
if not User.objects.filter(username=user_dict[i][2]).exists():
if user_dict[i][2]==None or user_dict[i][3]==None or user_dict[i][4]==None or user_dict[i][6]==None:
i=i+1
else:
external_user = User.objects.create(username=user_dict[i][2],first_name=user_dict[i][3],last_name=user_dict[i][4],email=user_dict[i][6])
i=i+1
#print('done')
else:
pass
i=i+1
print('done')
return render(request, 'account/testing.html', {'user_dict':user_dict}) ```