SqlAlchemy 中的动态表创建和 ORM 映射 [英] Dynamic Table Creation and ORM mapping in SqlAlchemy

查看:33
本文介绍了SqlAlchemy 中的动态表创建和 ORM 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用关系数据库还很陌生,所以我更喜欢使用好的 ORM 来简化事情.我花时间评估了不同的 Python ORM,我认为 SQLAlchemy 正是我所需要的.然而,我已经到了精神上的死胡同.

我需要创建一个新表,以配合我在应用程序的播放器表中创建的每个播放器实例.我想我知道如何通过元数据更改表的名称然后调用 create 函数来创建表,但我不知道如何将其映射到新的动态类.

有人能给我一些提示来帮助我摆脱大脑冻结吗?这甚至可能吗?

注意:如果我要问的更容易实现,我对 Python 中的其他 ORM 持开放态度.只要告诉我如何:-)

解决方案

我们被 SQLAlchemy 宠坏了.
以下内容直接取自教程
并且非常容易设置和开始工作.

而且因为经常这样做,
文档 a 中的完整引用 100c8

设置您的环境(我使用 SQLite 内存数据库进行测试):

<预><代码>>>>从 sqlalchemy 导入 create_engine>>>engine = create_engine('sqlite:///:memory:', echo=True)>>>从 sqlalchemy 导入表、列、整数、字符串、元数据>>>元数据 = 元数据()

定义你的表格:

<预><代码>>>>玩家表 = 表('玩家',元数据,... Column('id', Integer, primary_key=True),...列('名称',字符串),...列('分数',整数)……)>>>metadata.create_all(engine) # 创建表

如果您打开了日志记录,您将看到 SQLAlchemy 为您创建的 SQL.

定义你的类:

<预><代码>>>>类播放器(对象):... def __init__(self, name, score):... self.name = 姓名... self.score = 分数...... def __repr__(self):...返回<Player('%s','%s')>";% (self.name, self.score)

将类映射到您的表:

<预><代码>>>>从 sqlalchemy.orm 导入映射器>>>映射器(播放器,players_table)<映射器在 0x...;播放器>

创建一个玩家:

<预><代码>>>>a_player = Player('蒙蒂', 0)>>>a_player.name'蒙蒂'>>>a_player.score0

就是这样,您现在有了一张玩家桌.

I'm fairly new to using relational databases, so I prefer using a good ORM to simplify things. I spent time evaluating different Python ORMs and I think SQLAlchemy is what I need. However, I've come to a mental dead end.

I need to create a new table to go along with each instance of a player I create in my app's player table. I think I know how to create the table by changing the name of the table through the metadata then calling the create function, but I have no clue on how to map it to a new dynamic class.

Can someone give me some tips to help me get past my brain freeze? Is this even possible?

Note: I'm open to other ORMs in Python if what I'm asking is easier to implement.Just show me how :-)

解决方案

We are spoiled by SQLAlchemy.
What follows below is taken directly from the tutorial,
and is really easy to setup and get working.

And because it is done so often,
the documentation moved to full declarative in Aug 2011.

Setup your environment (I'm using the SQLite in-memory db to test):

>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite:///:memory:', echo=True)
>>> from sqlalchemy import Table, Column, Integer, String, MetaData
>>> metadata = MetaData()

Define your table:

>>> players_table = Table('players', metadata,
...   Column('id', Integer, primary_key=True),
...   Column('name', String),
...   Column('score', Integer)
... )
>>> metadata.create_all(engine) # create the table

If you have logging turned on, you'll see the SQL that SQLAlchemy creates for you.

Define your class:

>>> class Player(object):
...     def __init__(self, name, score):
...         self.name = name
...         self.score = score
...
...     def __repr__(self):
...        return "<Player('%s','%s')>" % (self.name, self.score)

Map the class to your table:

>>> from sqlalchemy.orm import mapper
>>> mapper(Player, players_table) 
<Mapper at 0x...; Player>

Create a player:

>>> a_player = Player('monty', 0)
>>> a_player.name
'monty'
>>> a_player.score
0

That's it, you now have a your player table.

这篇关于SqlAlchemy 中的动态表创建和 ORM 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆