从Python中的SQLAlchemy了解MetaData()


哦哦了
2025-03-21 12:23:37 (19小时前)
  1. 我想知道是什么


元数据
</跨度>
()创建对象本质上是。在Python中反映和创建数据库时使用它(使用SQLAlchemy包)。

考虑以下工作代码:

/ with preloaded Engine(sqlite:///chapter5.sqlite)和

元数据
</跨度>
=

元数据
</跨度>
():我打电话的时候

元数据
</跨度>
在控制台中,它返回’

元数据
</跨度>
(bind = None)’/

#导入表,列,字符串和整数

2 条回复
  1. 0# 誓言你好 | 2019-08-31 10-32



    我想你问过python(你可能想要的SQLAlchemy)如何将表连接到元数据,将元数据连接到数据库和引擎。



    因此,SQLAlchemy中的数据库表属于(链接到)元数据对象。该表将自己添加到元数据中;元数据对象上有一个表属性,与列表非常相似:



    rue,nullable = False),Column(’host_id’,Integer(),ForeignKey(’slots.id’),table =,nullable = False),Column(’active’,Boolean(),table =),Col
    umn(’port’,Integer(),table =,nullable = False),Column(’description’,String(length = 120),table =),Column(’username’,String(length = 40),tab
    le =),Column(’password’,String(length = 40),table =),schema = None),’network_location_associations’:表(’network_location_associations’,M
    etaData(bind = None),Column(’network_id’,Integer(),ForeignKey(’networks.id’),table =),Column(’location_id’,Integer(),ForeignKey(’locations。
    id’),table =),schema = None),’machines’:表(’machines’,MetaData(bind = None),Column(’id’,Integer(),ForeignKey(’items.id’),表=




    ,primary_key = True,nullable = False),Column(’eth0’,String(),table =),Column(’eth1’,String(),table =),Column(’eth2’,String(),table = ),Colu
    mn(’eth3’,String(),table =),Column(’wlan0’,String(),table =),Column(’ipmi’,String(),table =),schema = None),’machine_profiles’ :表(’马赫
    ine_profiles’,MetaData(bind = None),Column(’id’,Integer(),table =,primary_key = True,nullable = False),Column(’DisplayPort’,Integer(),table =),Column(’HDMI ‘,Integer(),table =),Column(’RAM’,String(length = 10),table =),schema = None),’geometry’:表(’geometry’,MetaData(b
    ind = None),Column(’slot_id’,Integer(),ForeignKey(’slots.id’),table =,primary_key = True,nullable = False),Column(’room_id’,Integer(),ForeignKey(’rooms) .id’),tabl
    e =,nullable = False),Column(’x_mm’,Float(),table =,nullable = False),Column(’y_mm’,Float(),table =,nullable = False),Column(’z_mm’,浮动(),t
    able =,nullable = False),Column(’rotation_deg’,Float(),table =,nullable = False),Column(’tilt_deg’,Float(),table =,nullable = False),Column(’ro
    ll_deg’,Float(),table =,nullable = False),Column(’on_floor’,Boolean(),table =,nullable = False),schema = None),’publicaddresses’:表(’publicaddresses’
    ,MetaData(bind = None),Column(’id’,Integer(),table =,primary_key = True,nullable = False),Column(’ip’,Integer(),table =,nullable = False),C
    olumn(’slot_id’,Integer(),ForeignKey(’slots.id’),table =,nullable = False),schema = None),’connections’:表(’connections’,MetaData(bind = None),Column (’一世
    d’,Integer(),table =,primary_key = True,nullable = False),Column(’src_slot_id’,Integer(),ForeignKey(’slots.id’),table =),Column(’src_index’,Inte
    ger(),table =),列(’src_type’,枚举(’HDMI’,’DisplayPort’,’miniDP’,’VGA’,’DVI’,’Power’,’CAT6’,’WallNet’,’Hybrid ‘,’UnknownVideo’),table =
    ),Column(’dst_slot_id’,Integer(),ForeignKey(’slots.id’),table =),Column(’dst_index’,Integer(),table =),Column(’dst_type’,Enum(’HDMI’) ,’显示
    yPort’,’miniDP’,’VGA’,’DVI’,’Power’,’CAT6’,’WallNet’,’Hybrid’,’UnknownVideo’),table =),schema = None),’types’:Table (’types’,MetaData(bind = None),Column
    (’type’,Integer(),table =,primary_key = True,nullable = False),Column(’name’,String(length = 60),table =),schema = None),’roles’:Table(’ roles’,MetaData(bind = No
    ne),Column(’id’,Integer(),table =,primary_key = True,nullable = False),Column(’name’,String(),table =,nullable = False),Column(’description’,String (),table =
    ,nullable = False),Column(’display_driver’,Boolean(),table =,nullable = False),schema = None),’rooms’:表(’rooms’,MetaData(bind = None),Column(’id ‘, 整数(
    ),table =,primary_key = True,nullable = False),Column(’location_id’,Integer(),ForeignKey(’locations.id’),table =,nullable = False),Column(’parent_id’,Integer(
    ),table =),Column(’name’,String(length = 50),table =),Column(’x_mm’,Float(),table =,nullable = False),Column(’y_mm’,Float() ,table =,nullable = F.
    alse),Column(’z_mm’,Float(),table =,nullable = False),Column(’rotation_deg’,Float(),table =,nullable = False),Column(’width_mm’,Float(),table =,null
    able = False),Column(’height_mm’,Float(),table =,nullable = False),Column(’depth_mm’,Float(),table =,nullable = False),Column(’has_workstations’,Boolean() ,ta
    ble =,nullable = False),schema = None),’displays’:表(’显示’,MetaData(绑定=无),列(’id’,整数(),表=,primary_key = True,nullable = False ),Co
    lumn(’hostname’,String(length = 100),table =),Column(’formation’,Enum(’2x3’,’1x2’,’corkboard’,’desktop’,’desktop-shared’),table = ,nullable = False),s
    chema = None),’slots’:表(’slots’,MetaData(bind = None),Column(’id’,Integer(),table =,primary_key = True,nullable = False),Column(’location_id’, Integer(),ForeignKey(’locations.id’),table =,nullable = False),Column(’hostname’,String(),table =),Column(’item_id’,Integer(),ForeignKey(’items.id’), table =),Column(’r
    ole_id’,Integer(),ForeignKey(’roles.id’),table =),Column(’parent_id’,Integer(),ForeignKey(’slots.id’),table =),Column(’ip’,Integer (),table =),Col
    umn(’ip_ipmi’,Integer(),table =),Column(’ip_wlan’,Integer(),table =),Column(’optional’,Boolean(),table =),Column(’notes’,String( ),table =),
    列(’分类’,枚举(’U’,’S’,’TS’),表=),列(’os’,String(),table =),列(’release’,String(),表=),列(’track’,String(),t
    able =),Column(’uuid’,Binary(),table =),Column(’displaydata’,String(),table =),schema = None)})





    LEN()models.Base.metadata.tables
    文件“”,第1行
    LEN()models.Base.metadata.tables
    ^
    SyntaxError:语法无效
    LEN(models.Base.metadata.tables)
    22






    您需要元数据对象的原因是:





    • 拥有一个单独的工作单元来创建和删除相关表





    • 有地方收集反射操作的所有结果





    • 基于依赖关系对相关表进行排序,以便可以按正确的顺序创建外键约束。





    因此,元数据对象包含它认为数据库可能类似的SQLAlchemy’sidea。它通常从反射或您创建表对象(可能通过声明性基本扩展)填充。



    您可以通过在元数据构造函数中设置bind参数,将元数据对象与实际数据库引擎直接关联。或者,您可以在创建调用或反射调用中使用元数据时建立链接。


登录 后才能参与评论