Separate related models in Sqlalchemy

Asked

Viewed 90 times

1

I have a model of relational tables:

models/Base.py

from collections import OrderedDict
from src.config.database import db


class Base(db.Model):
    __abstract__ = True

models/Model.py

from .Base import Base 
from src.config.database import db


class Estado(Base):
    __tablename__ = 'ESTADO'

    id = db.Column('EST_ID', db.Integer, primary_key=True)
    sigla = db.Column('EST_SIGLA', db.String, nullable=False)
    desc = db.Column('EST_NOME', db.String, nullable=False)

    enderecos = db.relationship('Endereco', backref=db.backref('estado', lazy='joined'), uselist=False)
    cidades = db.relationship('Cidade', backref=db.backref('cidade', lazy='joined'), lazy='dynamic')


class Cidade(Base):
    __tablename__ = 'CIDADE'

    id = db.Column('CID_ID', db.Integer, primary_key=True)
    desc = db.Column('CID_NOME', db.String, nullable=False)
    est_id = db.Column('EST_ID', db.Integer, db.ForeignKey('ESTADO.EST_ID'))

    enderecos = db.relationship('Endereco', backref=db.backref('cidade', lazy='joined'), uselist=False)


class Endereco(Base):
    __tablename__ = 'ENDERECO'

    id = db.Column('END_ID', db.Integer, primary_key=True)
    cep = db.Column('END_CEP', db.String, nullable=False)
    bairro = db.Column('END_BAIRRO', db.String, nullable=False)
    numero = db.Column('END_NUMERO', db.String, nullable=False)
    complemento = db.Column('END_COMPLEMENTO', db.String, nullable=False)

    est_id = db.Column('EST_ID', db.Integer, db.ForeignKey('ESTADO.EST_ID'))
    cid_id = db.Column('CID_ID', db.Integer, db.ForeignKey('CIDADE.CID_ID'))

I would like to separate the model by files, I have the file models/Model.py and wanted to separate the classes into models/Endereco.py, models/Estado.py and models/Cidade.py. But when I do, he always makes a mistake warning that the class doesn’t exist from the relationship. What’s the best way to do this? Or the correct thing would be to keep it in the same file?

  • What happens if in the file models.__init__.py you import the files with the templates in the desired order: from ..models import base, estado, cidade, endereco (also - avoid leaving the files with the first letter uppercase - may give problems with some tools - leave the names of the files always in lowercase)

  • the problem I think I’m going to have when doing this, is that it would be many classes for manual import, I have more than 50 tables, I would have to import one by one in the inside of the init.py ? or has another alternative?

  • if you don’t think you have 50 files much - it’s not much to import one by one. : -) In Python it is completely unnecessary to have u arqiuvo for each class - especially when some will have few lines and will appear only as relationships within a single class. But I saw here - yes, the problem is importing the modules - it doesn’t have to be in the right order - check out this answer: https://stackoverflow.com/a/31091883/108205

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.