Is this an adequate model of the database?

Hello to all my friends and newbies in python and decided to write her catalogue of places in Moscow. Wrote that file with models. I would like if someone could take a look whether the normal is generally composed of BD?)
Code
from sqlalchemy.dialects.postgresql import ARRAY, DATERANGE, JSONB
from sqlalchemy.sql import text, select
from sqlalchemy_utils import DateRangeType
from sqlalchemy import create_engine, Column, Integer, String, Text, ForeignKey, TIMESTAMP,
from sqlalchemy.the orm import sessionmaker, relationship
from sqlalchemy.ext.DRI import declarative_base
import config as cfg
import utils

from marshmallow_sqlalchemy import ModelSchema
from marshmallow import fields

SQLALCHEMY_DATABASE_URI = "postgresql+psycopg2://kell:124124fwqf32@%s/kell" % cfg.DOMAIN

db = create_engine(SQLALCHEMY_DATABASE_URI)
connection = db.connect()

Session = sessionmaker(bind=db)

db_session = Session()

Base = declarative_base()


class Company(Base):

 __tablename__ = "companies"
 company_id = Column(Text, primary_key=True)
 name = Column(Text)
 address = Column(Text, default="Moscow")
 about = Column(Text)
 status = Column(Text, default="enable")
 data = Column(JSONB)
 emblem = Column(Text, default="")
 city = Column(Text, ForeignKey("cities.code"), default="177.MSK")

 COMPANY_DETAIL_TEMPLATE = {
 "1": {"companyMenu": {"categories": [], "dishs": []}},
 "2": {},
 "3": {},
 "4": {},
 "5": {}
}

 def compose(self: Company, params: dict, create_user: bool = True):
 # Make a unique suffix of the company
 company_suffix = utils.crc32(params)

 self.company_id = "-".join(
 [params["city"], params["category"], company_suffix]) # Make id company

 self.name = params["name"]
 self.about = params["about"]

 self.data = self.COMPANY_DETAIL_TEMPLATE[params["category"]]
 self.address = params["address"]

 self.city = params["city"]

db_session.add(self)

 if create_user:
 user_params = {
 "role": 0,
 "login": self.company_suffix,
 "password": utils.generate_password(8)
}
 company_user = User(
company_id=self.company_id).compose(params=user_params)

db_session.add(company_user)


class User(Base):
 __tablename__ = "users"

 user_id = Column(Integer, primary_key=True)
 role = Column(Integer, default=-1)
 login = Column(Text)
 password = Column(Text)
 company_id = Column(Text, ForeignKey("companies.company_id"))
 company = relationship(Company)

 def compose(self: User, params: dict):
 self.role = params["role"]
 self.login = params["login"]
 self.password = params["password"]


class Post(Base):
 __tablename__ = "posts"

 post_id = Column(Integer, primary_key=True)
 user_id = Column(Integer, ForeignKey("users.user_id"))
 company_id = Column(Text, ForeignKey("companies.company_id"))
 title = Column(Text)
 data = Column(Text)
 release_date = Column(ARRAY(TIMESTAMP))
 address = Column(Text)
 img_url = Column(Text)
 second_img_url = Column(Text)
 tags = Column(ARRAY(Text))
 priority = Column(Integer, default=0)
 cost = Column(Integer, default=0)

 company = relationship("Company", lazy="joined")


class Tag(Base):
 __tablename__ = 'tags'
 tag_id = Column(Integer, primary_key=True)
 tag = Column(Text, primary_key=True)


class File(Base):
 __tablename__ = "files"
 file_id = Column(Integer, primary_key=True)
 company_id = Column(Text, ForeignKey("companies.company_id"))
 name = Column(Text)
 filename = Column(Text)
 url = Column(Text)


class City(Base):
 __tablename__ = "cities"
 code = Column(Text, primary_key=True)
 name = Column(Text)


class Category(Base):
 __tablename__ = "categories"
 id = Column(Text, primary_key=True)
 name = Column(Text)
 short_name = Column(Text)


class Route(Base):
 __tablename__ = "routes"
 id = Column(Integer, primary_key=True)
 name = Column(Text, default="Route")
 mapid = Column(Text)
 about = Column(Text)
 img_url = Column(Text)


class Place(Base):
 __tablename__ = "places"
 id = Column(Integer, primary_key=True)
 name = Column(Text, default="Place")
 address = Column(Text)
 about = Column(Text)
 img_url = Column(Text)
 company_id = Column(Text, ForeignKey("companies.company_id"))
 category_id = Column(Text, ForeignKey("categories.id"))
 tags = Column(ARRAY(Text))
 priority = Column(Integer, default=0)


# Marshmallow Schemas


class CompanySchema(ModelSchema):
 class Meta:
 model = Company

 fields = ("name", "company_id", "address", "about", "status", "data",
"emblem")


class UserSchema(ModelSchema):
 class Meta:
 model = User

 fields = ("user_id", "company_id", "role", "login", "password")


class PostSchema(ModelSchema):
 company_name = fields.Str()

 class Meta:
 model = Post

 fields = ("post_id", "company_id", "user_id", "title", "data",
 "release_date", "address", "img_url", "second_img_url",
 "tags", "priority", "cost")


class TagSchema(ModelSchema):
 class Meta:
 model = Tag


class CitySchema(ModelSchema):
 class Meta:
 model = City


class FileSchema(ModelSchema):
 class Meta:
 model = File

 fields = ("file_id", "company_id", "name", "filename", "url")


class CategorySchema(ModelSchema):
 class Meta:
 model = Category


class RouteSchema(ModelSchema):
 class Meta:
 model = Route


class PlaceSchema(ModelSchema):
 class Meta:
 model = Place

 fields = ("img_url", "id", "name", "address",
 "about", "company_id", "tags", "priority")


def result_to_json(result, schema):
 return schema().dump(result).data
March 12th 20 at 08:10
2 answers
March 12th 20 at 08:12
Well here you for example search by address...all of you rested in the database... the query of course will do better but so not necessary.
Limit from the margins.

And opalites tips @Keanu.Rowe83 and @Stasgar
March 12th 20 at 08:14
Once again Express my meaning more specifically, if mentioned :-)
Base is designed for typical queries, and not just. To speed up their use of normalization. In other words - first description of a business model, then query, and then tables, indexes, and so on. Of course 1000-100000 records none of this can not feel good computeri are not the same. But in six zeros - the result is felt, and how.
So draw a diagram and write queries words. As an example: the user needs to see the warehouses of firms within a radius of 50km from your location, sorted by distance and filtered by relevancy products from. 1 to 5.
Few did not understand why I rested in the database? - lue98 commented on March 12th 20 at 08:17
@lue98, the scheme of the scheme of discord, which is good for some queries, does not fit others. For example, we had provisiona table with key-value, everything was fine, until you need in each query to do the grouping... had to redo the schema and the tables and the data.
Another example, indices, they are good exactly as long as their reasonable amount and they are not thermoset insert the data. - Keanu.Rowe83 commented on March 12th 20 at 08:20
@lue98, and Yes, I'm not saying that rested in the database, and say that a poorly made diagram on large volumes may simply to run very slowly. - Keanu.Rowe83 commented on March 12th 20 at 08:23
@Keanu.Rowe83, and my scheme is now bad? Or is it just the explanation for the future? - lue98 commented on March 12th 20 at 08:26
@lue98, don't mean to be rude, I already wrote in the comments that:
- the code to restore the data scheme - more fun then!
- your code did not see as You do your data will be used and what queries are.

If You want to get an adequate response then do the following:
1) draw a diagram of the tables in the form of a drawing or pictures!
2) describe at least two words of the task itself, the results that you want from the Datasheet!
Without this it is impossible to say whether the scheme or not! - Keanu.Rowe83 commented on March 12th 20 at 08:29

Find more questions by tags SQLAlchemyPostgreSQLPython