-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_reflection.py
More file actions
102 lines (82 loc) · 3.41 KB
/
Copy path06_reflection.py
File metadata and controls
102 lines (82 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""06_reflection.py - Runtime schema reflection with SQLAlchemy Inspector API.
Demonstrates:
- Creating sample tables at runtime
- Using inspect() to discover tables, columns, PK/FK, and indexes
- Printing reflected schema details clearly
"""
from __future__ import annotations
from sqlalchemy import Column, ForeignKey, Integer, MetaData, String, Table, create_engine, inspect
DATABASE_URL = "cubrid+pycubrid://dba@localhost:33000/testdb"
def main() -> None:
engine = create_engine(DATABASE_URL)
metadata = MetaData()
authors = Table(
"cookbook_ref_authors",
metadata,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("author_name", String(100), nullable=False),
)
books = Table(
"cookbook_ref_books",
metadata,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("author_id", Integer, ForeignKey("cookbook_ref_authors.id")),
Column("title", String(120), nullable=False),
Column("price_cents", Integer, nullable=False),
)
print("=== SQLAlchemy Reflection Demo ===")
try:
metadata.create_all(engine)
print("[1] Created tables cookbook_ref_authors and cookbook_ref_books")
with engine.begin() as connection:
_ = connection.execute(
authors.insert(), [{"author_name": "Kim"}, {"author_name": "Lee"}]
)
_ = connection.execute(
books.insert(),
[
{"author_id": 1, "title": "SQL Basics", "price_cents": 2500},
{"author_id": 1, "title": "ORM Practical", "price_cents": 3300},
{"author_id": 2, "title": "CUBRID Guide", "price_cents": 2900},
],
)
inspector = inspect(engine)
print("\n[2] Table discovery")
tables = sorted(
name for name in inspector.get_table_names() if name.startswith("cookbook_ref_")
)
for table_name in tables:
print(f"- {table_name}")
for table_name in tables:
print(f"\n[3] Columns for {table_name}")
columns = inspector.get_columns(table_name)
for column in columns:
nullable = "YES" if column.get("nullable", True) else "NO"
print(
f"column={column['name']}, type={column['type']}, nullable={nullable}, default={column.get('default')}"
)
pk = inspector.get_pk_constraint(table_name)
print(f"Primary key: {pk.get('constrained_columns', [])}")
indexes = inspector.get_indexes(table_name)
if indexes:
for idx in indexes:
print(
f"Index: {idx.get('name')} columns={idx.get('column_names')} unique={idx.get('unique')}"
)
else:
print("Index: none")
fks = inspector.get_foreign_keys(table_name)
if fks:
for fk in fks:
print(
f"Foreign key: {fk.get('constrained_columns')} -> {fk.get('referred_table')}.{fk.get('referred_columns')}"
)
else:
print("Foreign key: none")
print("\nReflection demo completed.")
finally:
metadata.drop_all(engine)
engine.dispose()
print("Cleanup complete.")
if __name__ == "__main__":
main()