Comment lire les définitions de table générées dynamiquement à l'aide de SQL Alchemy
Je veux faire le titre! Je l'ai recherché, alors j'ai pris note. En conclusion, ce n'est pas grave si vous créez un objet de mappage à l'aide du type de fonction intégré. Par exemple, écrivez comme ceci.
engine = create_engine(URL de la base de données, echo=True, encoding='utf-8')
base = declarative_base(engine)
dic = {
'__tablename__':nom de la table,
'__table_args__': {'autoload': True}}}
obj = type('MyObject', (base,), dic)
Le but est de définir autoload = True
pour créer un mappage à partir d'une table existante.
sql_sample.py
# -*- coding: utf-8 -*-
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
schema = 'sqlite:///test.db'
engine = create_engine(schema, echo=False, encoding='utf-8')
base = declarative_base(engine)
session = sessionmaker(bind=engine)()
management_dic = {
'__tablename__': 'management',
'__table_args__': {'autoload': True}}
management_object = type('management_object', (base,), management_dic)
fetch_management = session.query(management_object).all()
for management_row_object in fetch_management:
folder_table_dic = {
'__tablename__': management_row_object.folder_table_name,
'__table_args__': {'autoload': True}}
folder_object_name = management_row_object.folder_table_name.encode('utf-8')
folder_object = type(folder_object_name, (base,), folder_table_dic)
fetch_folder = session.query(folder_object).all()
print "-" * 32
print "table_name: %s" % folder_object_name
print "-" * 32
for row in fetch_folder:
print " fid: %s, file_name: %s" % (row.fid, row.file_name)
print
tid | folder_table_id |
---|---|
0 | folder_0000 |
1 | folder_0001 |
management.sql
CREATE TABLE management(
tid INTEGER NOT NULL PRIMARY KEY,
folder_table_name TEXT NOT NULL);
INSERT INTO management(tid, folder_table_name) VALUES (0,'folder_0000');
INSERT INTO management(tid, folder_table_name) VALUES (1,'folder_0001');
fid | file_name |
---|---|
0 | ham.txt |
1 | spam.txt |
2 | egg.txt |
folder_0000.sql
CREATE TABLE folder_0000(
fid INTEGER NOT NULL PRIMARY KEY,
file_name TEXT NOT NULL
);
INSERT INTO folder_0000(fid, file_name) VALUES (0,'ham.txt');
INSERT INTO folder_0000(fid, file_name) VALUES (1,'spam.txt');
INSERT INTO folder_0000(fid, file_name) VALUES (2,'egg.txt');
fid | file_name |
---|---|
0 | foo.txt |
1 | bar.txt |
folder_0001.sql
CREATE TABLE folder_0001(
fid INTEGER NOT NULL PRIMARY KEY,
file_name TEXT NOT NULL
);
INSERT INTO folder_0001(fid, file_name) VALUES (0,'foo.txt');
INSERT INTO folder_0001(fid, file_name) VALUES (1,'bar.txt');
sqlite3 test.db < management.sql
sqlite3 test.db < folder_0000.sql
sqlite3 test.db < folder_0001.sql
python sql_sample.py
--------------------------------
table_name: folder_0000
--------------------------------
fid: 0, file_name: ham.txt
fid: 1, file_name: spam.txt
fid: 2, file_name: egg.txt
--------------------------------
table_name: folder_0001
--------------------------------
fid: 0, file_name: foo.txt
fid: 1, file_name: bar.txt
Python> Documentation> Bibliothèque standard Python> 2. Fonctions intégrées Dynamically setting _tablename_ for sharding in SQLAlchemy?
Recommended Posts