sqlalchemy metadata

Solutions on MaxInterview for sqlalchemy metadata by the best coders in the world

showing results for - "sqlalchemy metadata"
Giorgio
24 Mar 2018
1# access the column "EMPLOYEE_ID":
2employees.columns.employee_id
3
4# or just
5employees.c.employee_id
6
7# via string
8employees.c['employee_id']
9
10# iterate through all columns
11for c in employees.c:
12    print(c)
13
14# get the table's primary key columns
15for primary_key in employees.primary_key:
16    print(primary_key)
17
18# get the table's foreign key objects:
19for fkey in employees.foreign_keys:
20    print(fkey)
21
22# access the table's MetaData:
23employees.metadata
24
25# access the table's bound Engine or Connection, if its MetaData is bound:
26employees.bind
27
28# access a column's name, type, nullable, primary key, foreign key
29employees.c.employee_id.name
30employees.c.employee_id.type
31employees.c.employee_id.nullable
32employees.c.employee_id.primary_key
33employees.c.employee_dept.foreign_keys
34
35# get the "key" of a column, which defaults to its name, but can
36# be any user-defined string:
37employees.c.employee_name.key
38
39# access a column's table:
40employees.c.employee_id.table is employees
41
42# get the table related by a foreign key
43list(employees.c.employee_dept.foreign_keys)[0].column.table