What is difference between users_user_permissions and auth_permission and auth_group_permissions? - django

Django autogenerated above tables with syncdb, but I can't understand what are those tables for.
Sorry for silly question, but I'm just started with groups and permissions.

Django's scheme for automatically generating table names is explained here. Basically, it uses the pattern app_model. In the case of ManyToManyFields—which are implemented by creating a new table to hold the relationship—this becomes app_model1_model2s.
So:
auth_permission is the table that represents the auth.Permission model.
auth_group_permissions is the table that represents the ManyToMany relationship between auth.Group and auth.Permission.
users_user_permissions is the table that represents the ManyToManyField between users.User and auth.Permission. (I assume this is coming from an app you're using? Django's contrib.auth version should be auth_user_user_permissions.)
See the documentation for how these tables are actually used.

Related

Does Django support setting the beginning value for an id column?

I have seen several questions and answers on SO, most were three years old or older and I looked at the Django documentation (hoping I didn't miss it). I have to have a 9+ digit number for an id. Most responses were to do this at the database. I am guessing that means to create the model in Django and then go back to the database and change the id column Django created with a new starting/next value attribute on the column.
If not how can I create a database table from Django, Code First, that allows me to create a table with an id column that starts at 100000000? And, it be done with the stock model object methods in Django. I don't really want to do a special hack. If that is the case, I can go the database and fix the column. I was trying to adhere to the Code First ideas of Django (though I prefer database first, and am afraid using inspectdb will make a mess.)
Edit: I didn't want to use UUID. I believe BigAutoField is best.
You should be able to do this in two steps:
1 - Specify your primary key explicitly using primary_key=TRUE in your model definition. See the Django docs for more info. You can then specify BigAutoField or whatever other type you want for the primary key.
2A - If you're populating the database up front, just set pk: 100000000 in your fixture.
OR
2B - If you're not populating the database up front, use Django Model Migration Operations RunSQL as detailed here. For your SQL use ALTER TABLE tableName AUTO_INCREMENT=100000000.

Django OneToOneField on foreign table

I'm trying to setup a new server with foreign tables (using postgres_fdw) that weren't foreign tables previously, and I have some OneToOneFields pointing to these tables. This doesn't work out of the box - OneToOneFields use foreign keys, and postgres_fdw does not support foreign keys for foreign tables.
The foreign tables are in a read-only database on the same server.
Is there an easy way to get this working?
After a little more research, I think the answer is 'don't do that'. Handling foreign keys for foreign tables isn't implemented, and any solution that tried to bridge that gap would have serious performance and/or consistency issues.
pglogical looks like a better solution - instead of pulling the data in through postgres_fdw, replicate the tables into the new database so they're local. There are other projects for replicating just specific tables, but pglogical is included with PostgreSQL 10.

What internal tables are automatically created by Django?

I'm working on blacklisting these for the data analysis tool Metabase.
I can't find any lists anywhere that specify which tables the framework creates.
So far from looking at some projects I have:
django_admin_log
django_content_type
django_session
django_site
south_migrationhistory
Am I missing anything?
Depending on your database, you could find the appropriate command and see all the tables created.
PostgreSQL :
\list or \l: list all databases
\dt: list all tables in the current database
MySQL :
show tables;
Similarly for other db SQL, Oracle
The actual list might depend on which apps have been enabled and third party plugins are used. If you want to find all the tables (without any 3rd party app), easiest way might be to create a project and enable all the apps in settings.
Here's an example for Django 1.9 :
auth_group auth_group_premissions auth_permission django_content_type
django_migrations django_session django_site user user_groups
user_user_permissions

Django Multiple DB with Contenttype table

well, I am implementing a multiple db feature on an exist django project. After I create and setup the secondary db to django, the first thing I did is running syncdb and migrate, which will create a lot built-in django tables, includes django_content_type table.
Then I realize, the records in the new django_content_type table are different from the one in primary db. To be more specific, the primary key is different.
Let's say I have a record in primary db django_content_type table looks like following:
id | name | app_label | model
----+------+-----------+-------
33 | Tag | taggit | tag
However, after syncdb and running migrations, I got following in my secondary db:
id | name | app_label | model
----+------+-----------+-------
11 | Tag | entities | tag
I kind of hate to have two inconsistent django_content_type sitting in my two db. I tried to truncate one of them and load a fixture from another and failed, since django_content_type table is referred by most of other tables.
What is best way to handle things like this? I try to convince myself this won't matter, but I am afraid inconsistency will bite me in the future, so I really want to solve this issue.
Thanks in advance.
It's a common issue since you are using different databases. The main concern here is when some models are using ContentType as a field which will be translated to an FK like content_type_object_id in the database, and when those are different in the databases might cause an error. Therefore, because you need two databases in your application you should manager your calls using db_manager() not using()
For example:
In your application, this might cause an error
ContentType.objects.get_for_model(Tag)
Because you are using a manager method get_for_model() and you need to specify the database before.
ContentType.objects.db_manager(...).get_for_model(Tag)
In this way you will make sure that you are getting the ContentType based on the PK that has been addressed in the specified database.
I am not saying that there is no solution, there might be a solution to sync the two databases. However, I wrote this to not worry you about the situation.
I really prefer that they would be consistent, but multiple databases never do to me.

Is it possible to create model of a table from a given table of database in django model

I want to create Only model of single database, Through InspectDB we can create model of complete database. But I am not very clear how to create one table model in Django?
I don't think you can tell inspectdb to generate code only for a certain table. You can just use inspectdb for all tables, and then only use the code generated for the table you want. The modelname usually is the CamelCased version of your underscored_table_name.