Reference constraint in oauth table Laravel 5.6 - laravel-5.5

I am using Laravel 5.6
I have 2 tables. User Table and Role Table in migration folder. Also installed Laravel Passport
When I ran this command php artisan migrate, I saw the auth tables created first and then role table and user table.
Can I run role and user table first because I want to put reference constraints in auth table?

I don't know the exact stuff Laravel Passport does, but in general the Migrator classes are ran in alphabetic order. Given the fact that they are prefixed with the generation timestamp, it should be enough just renaming the role migrator, to having a timstamp before the user migrator.
When you do this, don't forget to regenerate the autoload files.

Laravel Migration run on the alpahbetical order .
Consider an example where you Auth table with the migration named as
2018_03_18_12_create_auth_tables.php
User table migration as
2018_03_18_13_create_users_tables.php
In this case the Auth table will run first and User table will run second due to their alpahbetical order . If you want to change the order of the migration then you can rename the file of users table as
2018_03_18_11_create_users_tables.php
After doing this the alphabetical order will change and the users table will run first.
I hope this helps

Related

Django Microservices - skip all user created tables for non-user services

I am using Django in a Microservice architecture. I already have a userService for authentication with JWT with an apiGatewayService that checks if token is valid before routing requests to other services. This means that I do not need all the standard tables that are created for a user (user tokens, sessions, email etc) in my other services when I run python manage.py migrate.
Is there a way to opt out of creating these unnecessary tables?
I have read some people just saying not to run migrations so as not to create these tables, but I obviously want to create other tables (say I have an ordersService, I want an orders table etc).
If you don't want that functionality at all, then remove the relevant apps from INSTALLED_APPS.

Django: Saving oauth provider data

I set up a simple django site using django-allauth.
I created some oauth providers in the database.
Everything is fine and working on my laptop now.
I would like to store the created database tables somehow.
Use case: I want to set up a new development environments on a different PC painlessly.
How to store the initial data of django_allauth, so that after checking out the app from git the command manage.py migrate is all I need to have the relevant database tables filled?
Django_allauth already save those data to the database, you will find them in a table *_SocialApp, here is the model code from django_auth source

Can't see permissions for new model in Django's admin interface

I registered a new model in Django's admin interface but I can't see any permissions related to it that I can assign to users or groups.
Could it be related to the fact that my models come from a different database?
I fixed a very similar issue today where I couldn't assign Users permissions concerning tables that were created in multiple databases because those tables didn't appear in the list of "available permissions."
It appears that I accidentally migrated the model creation migrations to the default database before I correctly used the --database DATABASE flag with manage.py migrate. So I had the same table names in both the default and auxiliary databases. I dropped the tables in the default database, leaving only the tables in the auxiliary database, and then the tables appeared in the permissions list.

"Django documentation" says "ensure that Django has permission to create and alter tables" so how do I do that in postgreSQL?

I'm taking EdX classes that use Ruby on Rails and python. That has given me courage to try and install and learn Django using Apach, mod_wsgi, and PostgreSQL. Following the detailed installation instructions, I first installed Apache, then mod_wsgi, and then PostgreSQL. I installed each from source and went through a little bit of tutorial with each and made sure they were properly installed. I've got a postgres user setup to run the PostgreSQL server and I was able to create a user "role" for myself as well as an admin role that my role inherits from that can create a database etc. I tried out some SQL in psql following a tutorial to make tables etc. I know how to grant privileges for a given role.
So anyway, I'm pretty close to the step where I would actually install Django from source, but I'm not sure how to follow this advice from the installation instructions:
If you plan to use Django's manage.py syncdb command to automatically create database tables for your models, you'll need to ensure that Django has permission to create and alter tables in the database you're using; if you plan to manually create the tables, you can simply grant Django SELECT, INSERT, UPDATE and DELETE permissions.
Maybe after I follow the steps to actually install Django and go through some tutorials, I'll understand exactly what needs to be setup in PostgreSQL to grant Django those permissions, but if I follow the installation instructions in order, it would seem to be saying I should setup those permissions now before installing Django. If I can get someone to tell me how to do it here before I do the install of Django, I'd appreciate it.
In the settings.py file of a django project, there is a snippet that says something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproj_db',
'USER': 'myproj_user',
'PASSWORD': '123',
'HOST': '',
'PORT': '',
},
}
What this does is that it tells Django what user (postgres user in this case) and database is used in conjunction with your django project.
Normally, you will need to create this myproj_user together with the myproj_db.
When you create this user, you can choose to give it permissions like so:
CREATE USER myproj_user WITH PASSWORD '123' SUPERUSER CREATEDB CREATEROLE LOGIN;
This creates the myproj_user with superuser, createdbm createrole, login permissions allowed to the user.
And then the database like so:
CREATE DATABASE myproj_db WITH OWNER myproj_user TEMPLATE=template1 ENCODING='utf-8';
You say that you know how to grant privileges for a given role, which is what you need to do for Django before installing it (running syncdb).
This is part of setting up your database for use with Django – a step you take before creating each Django project. Each Django project corresponds to a site that you build with Django and is completely separate from another Django project. To each project belongs a database.* You can install Django the framework before you setup your database, because Django the framework doesn't do anything on its own.
Either you give Django permissions to create tables for you, in which case it can create tables for you (using manage.py syncdb). Or, you use manage.py sqlall <app> to get SQL that you run yourself to create the tables needed (which might be nice if you're paranoid about security).
To grant all permissions to a user for a specific database (option 1) in Postgres, use the command (from psql):
GRANT ALL PRIVILEGES ON DATABASE <db-name> TO <username>;
* Technically, they can share a database by simply configuring them to use the same one.
Django uses ORM (Object Relational Mapper). What that means is that you do not directly deal with database tables for querying things however you deal with certain classes which then deal with the database. This is very useful and much more user-friendly then doing manually SQL. Consider the following example:
SELECT * FROM foo WHERE field="value"
vs
Foo.objects.filter(field='value')
In ORM, you describe the tables you have by making certain classes (in Django we call them models) and those models correspond to tables in the db, and their fields correspond to the columns in the db table. The following are very similar:
CREATE TABLE foo (
title varchar(50),
description text,
likes integer
);
and
class Foo(models.Model):
title = models.CharField(max_length=50)
description = models.TextField()
likes = models.IntegerField()
However it is waste of time for you as a developer to construct the SQL statements for creating tables, and describing those tables in Python as models. Not to do that, Django allows you once you define all your models, to create db tables for you. That is the purpose of the syncdb command. It takes all the installed apps and models within them and creates tables within your database for them. However as you already mentioned in your question, databases have roles and those roles have permissions. For example, one of the permissions is CREATE as described here. So what Django documentation is saying is for you to grand all necessary permission to a role which Django will use to interact with the db for Django to be able to create necessary db tables it needs as well as possibly modify them later.

Tying a model to a specific database alias

I have defined two databases in my settings.py with default, cas.
In my accounts application models.py I have created two clases
UserProfile and Users.
I want to tie UserProfile table to default and Users to cas db setting:
so for e.g. when I do a syncdb using the following command
python manage.py syncdb --database=cas
it should create only the users table in CAS and not the UserProfile table too.
Is there a way I can achieve this?
Take a look at the Database routing features of Django 1.2. You'll likely find what you need:
http://docs.djangoproject.com/en/dev/topics/db/multi-db/#automatic-database-routing