Django tables get generated with domain\username prefixed before table name - python

I am using Microsoft SQL server with Django (1.8.4). When I run migrations, the tables get created as
domain\username.table_name1
domain\username.table_name2
instead of
table_name1
table_name2
How do I resolve this? Is there a configuration for the naming format?
Django is running on Ubuntu 14.04.

This is by design, so you could have similar models in multiple applications. Otherwise, two application might use an "Expenses" or "Songs" model, and the DB will fail to create two tables with the same name.
You should usually stick with the default naming scheme.
The db_table Meta option does let you specify the table name, but it is mostly for existing legacy database.

Alter user's default schema you are connecting with. When you create a user and allow it to access a database, server create a schema of the same name and sets it as user's default schema. If you did not set one explicitly.Django does not work with schemas and does not try to create "dbo.MyTable" - it attempts to create "MyTable" and finally it goes to the default schema which is your username.

Related

use existing database instead of django database

i want to connect my existing database of sql server with django but the problem is django has its model which create its own database but i dont want to create database using django i just want to use mine to retrieve data.
the one solution i saw was to use inspectdb but hte problem with inspectdb is that it does not pick wring keys and constraints sometime plus have to set many thing manually but in my project my database is user defined user will connect its database so i dont actually know that how many and what table user's database have do i just want to connect that database with django and use the value of it.
my existing database is sqlserver.
any technique to use my existing database without using django database and retrive data from existing database.
thankyou.
As you mentionned, you should use : inspectdb to create your models as stated in the doc (https://docs.djangoproject.com/en/4.0/howto/legacy-databases/)
Setting the managed option to False in the Meta class of model, you can instruct Django to not make any migrations or database schema modification, but you need to tweak the model yourself to ensure every kind of mapping is as you intendend, and obviousl ensure coherence between your DB schema and models.

View Django User table from MySQL console

I want to view Django default User table from MySQL console.
I know to access from django shell or python by simply importing it.
from django.contrib.auth.models import User
is there a way to view it from MySQL console itself?
and where it will be located? i mean in which database django user table belongs?
Of course you can see it in the database.
As with all other models, unless instructed otherwise by using a db_table property in the Meta class, Django uses the naming schema appname_modelname for tables - so in this case the table is auth_user.
If you don't know and can't find the source, you can ask the model itself - ie User._meta.db_table.
The tables are located in the database which you have specified in settings.py. The django user table will be located at yourdbname.auth_user. All user defined models will be stored as yourdbname.appname_modelname
You can use "python manage.py dbshell" (in order to do this in linux, you need to install mysqlclient) and use "show tables;" and "select * from yourdb.table"

Django Rebuild all migrations

I am building an app in Django and it uses a live/in-use database.
Basically since the apps development the SQL Database has undergone some structure changes and it is causing issues with Django, Django will try to apply migrations to the database that already exist. For example:
In the Django app I marked the email column as unique which was fine based on the development database. However the main database now always has a table change that marks the email column as unique. Django is fighting this unique key with the one that already exists.
So is it possible to clear all Django migrations and have it make migrations again compared to the more up to date SQL database structure?
If your models are very out of sync with your database, the easiest option is probably to rebuild your models from scratch using inspectdb.
If your models are pretty close to the database already, the first step is to make sure that your models match the database exactly. You can use sqldiff from django-extensions for this.
Once your Django models match your database, follow this answer to re-create migrations based on the existing database schema.

How does dependencies isolation between MongoDB and Django work?

How does Isolation dependencies between mongdb and django work?
I have made several projects with JAVA/SPRING, and the recent days,
I am studying Python django .
I successed to get connection between django and sqlite, and few days ago,
when I tried to connect to mongodb, it is hard to seperate dependencies database and django because of django-admin.
Django-admin require to attain specific Fields to each model, however, every database has each their own Field properties. Therefore, when project should change database structure, we should change a lot of code in model.py.
e.g
What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
Is it Okay? do I miss something?
What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
TextField and StringField isn't where you will have problems since it is supported in almost all databases, but consider this library django-mongodb-engine, it reuses the basic fields that already exist in Django ORM to the extent of what's possible, e.g.
class Post(models.Model):
title = models.CharField() <-- both Django ORM and mongodb-engine have this
text = models.TextField() <-- both Django ORM and mongodb-engine have this
This model will work as is both in SQL and MongoDB engines, but if you use a MongoDB-only (as in not in default Django ORM engine) feature like ListField()
class Post(models.Model):
title = models.CharField()
text = models.TextField()
tags = ListField() # <-- not supported everywhere
Then you migration to a SQL engine will involve some manual work to map that field.
Therefore, when project should change database structure, we should change a lot of code in models.py. Is it Okay?
Typically yes, to change the database structure you make changes in models.py. After which you would be running a migration, which is a step that generates scripts to alter the database (e.g. adding a new SQL table column). For MongoDB there's no explicit migration, as its schema is flexible (or schemaless if you prefer).
As for the admin, it's fairly smart when it comes to Django models, you may need to define the models and fields you want to appear in the admin, but in the detail view you don't have to worry about every single field, as Django admin understands them based on the information in models.py
And to change database drivers, it's the DATABASES value in settings.py

How do I set prifix to django contrib tables names?

HI
One of my requirement is to have prefix on all the tables of the django based project (Because db is hosted on shared server). I have used db_table Meta option to set the prefix for the tables which I have created.
Now my query is how do I set the prefix for tables provided by django.contrib.
Instead of auth_group django should create prefix_auth_group.... How do I do that?
Thank you very much...
From reading this ticket, I would say it is not possible without a hack.

Categories

Resources