Working with multiple databases from the same class model

0

I'm new to Python and I need to implement a solution that every system user must have a separate database, briefly what I need is something like

db1 = Persistencia('mysql', 'usuario','minhasenha', '192.168.0.1','banco1')    
db2 = Persistencia('mysql', 'usuario','minhasenha', '192.168.0.1','banco2')    

#mapeia os models para o banco de dados
db1.mapeiaModels([Pessoa, Contato])
db2.mapeiaModels([Pessoa, Contato])

#atualiza a estrutura do banco de acordo com o mapeamento, 
#adiciona ou altera colunas NUNCA apaga colunas
db1.sync() 

pessoa = Pessoa()
pessoa.nome = 'RODRIGO'
pessoa.telefone = '88888888'

db1.save(pessoa) #salva no banco de dados 1
db2.save(pessoa) #salva no banco de dados 2

I need to have this control at the code level, logically I do not want to develop an ORM, I just want to make a wrapper of one of the existing ORM frameworks, I took a look at Django, PyPone and SQLAlchemy, but not I am trying to create a new class in my Python class and I want to be able to create a new class in my Python class. p>

Note: I need to use on the same server but on Persistencia separated as shown in the example, in the case databases and banco1 and it is crucial to update the schema with minimum effort, ie, the framework that will be used must have this feature

    
asked by anonymous 23.07.2015 / 23:14

1 answer

0

You have some options on how to do this, if you use Django you can use the models themselves, otherwise the ORM most popular for python is SQLAlchemy .

Using SQLAlchemy you can create multiple connections:

engine1 = create_engine('sqlite:///example1.db')
engine2 = create_engine('sqlite:///example2.db')

And take advantage of the same models just taking care of the time when you want to make the querys with the models use the session for the correct connection.

    
07.08.2015 / 03:15