For LB3 there is specific documentation on how create database tables for built-in models, but for LB4 it seems missing, the closest thing I could find is this page in the migration guide, but it just explain the difference in models persistence handling and the advantages on having the repositories handling it instead of the models themselves.
Moreover, already having a mysql-based datasource, trying to create a repository to handle Users persistence result in error since it seems not to find the User built-in model.
How can I create table for built-int models, e.g. User, in LB4?
Update #1
Found this page in the documentation that tells how to list models to migrate and to use npm run migrate so I've tryied to put User in the list but got the foolowing error: so it seems to find/recognize the model but complains about it not having datasource settings in the model() decorator (I guess).
root#71827bda8df9:/home/node/app# npm run migrate
> backend#0.0.1 premigrate
> npm run build
> backend#0.0.1 build
> lb-tsc
> backend#0.0.1 migrate
> node ./dist/migrate
Migrating schemas (alter existing schema)
Cannot migrate database schema Error: Cannot migrate models not attached to this datasource: User
at /home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:1146:12
at processTicksAndRejections (node:internal/process/task_queues:78:11)
Long story short
Cannot create tables for built-in models.
Why?
Found this SO question with a similar problem, and the accepted answer links to this docs page where built-in models are still marked as TBD almost 4 years later.
So what now?
I guess I have to create my own user model and integrate it with what LB4 offers me to handle users authentication.
Related
In a Django project, I have a few apps and one model-class will be created in one app's models.py and imported to another apps' models.py. then I run migrate for each app per one schema in Postgres, there is no error reported.
But the issue is the shared model will be migrated to all the schemas because (I assume this is the reason) I imported this model in each app's models.py.
Anyone knows How to avoid or solve this problem. Because I would like keep the model only stay in his original schema.
Or can I delete this model directly from each schema it appears?...without any side effects.
Please help, Thanks!
I found a better answer: Installing PostgreSQL Extension to all schemas.
should see this before.
Accidentally found the solution, just use 'public' schema for the app where models supposed to import to other apps.
Just for someone who experience the same situation.
I am working on already existing data on relational database. Now question is that how to build models and how to update the tables with new data coming from user (technically django forms)?
Django natively supports creating models for and working with existing data. From the documentation:
Integrating Django with a legacy database
Django will still need to create several of its own tables, but will adapt to use your existing tables. From the doc, you can auto-create models like this:
python manage.py inspectdb > models.py
You'll need to determine whether you want to manage updates to the table structure, but that's getting into details that will be specific to your project.
I'm creating a bus booking website using Django. I'm stuck at what the database tables should look like. So far I've created the displayed tables, I'm very poor with databases so please mention foreign keys and primary keys when answering. Also, I'm stuck on how to actually book seats in Django, like what will be the code in the background and what other packages should I use. Thanks.
Table- Route:
Columns - route_id (primary key),
location_from,
location_to,
route_name,
Table - Bus:
Columns - Bus_id(primary key), type_of_bus,
bus_registration,
capacity,
bus_number,
route (foreign key)
Also I'm using the default sqlite database that comes with django. Is it good enough to build this kind of website or do I need to change? This website is just a project and will never go into production phase.
I'd highly recommend writing your database schema in Python classes (as Django models) and then using ./manage.py makemigrations ./manage.py migrate, which will look at your model code and create the corresponding database schema. This portion of the django tutorial could be helpful to you.
The python code corresponding to your example for a route would look something like:
from django.db import models
class Route(models.model):
location_from=models.CharField()
location_to=models.CharField()
route_name=models.CharField()
This is a much easier way to write a database schema, especially if you're not so hot with databases.
If your app will never be in production, SQLite is fine - it's very similar to other database engines for toy projects.
As for what your data model should look like, I think it depends how complex you want to make the app. For a start, I think you could add a Location model, which your Route model should reference. For making reservations, you could look into django booking - I haven't used it but it comes up on django packages when you search 'booking'.
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.
I've built an application that I want to move from my development server to my production server. In this application I have defined 3 custom groups in auth.group and each of those have specific permissions.
I've tried to dump the data from auth.group - it seems to include permissions ids as well. The problem is, those IDs don't match between my development environment and the production environment. It also seems there is a content_type_id in auth.permission that I don't know how it relates.
My question is, is there a way using dumpdata or something else, to migrate Groups and all of the related permissions for my application? I don't have a problem importing multiple fixtures on the production server, but I do want all of the groups to be set up without having to go through the UI and selecting the appropriate permissions for each group.
django.contrib.auth depends on django.contrib.contenttypes because auth.models.Permission.content_type is a ForeignKey(ContentType).
Solution: add ContentType in your data dump, ie. dumpdata with the following arguments: auth.group contenttypes.contenttype auth.permission