sqlalchemy.exc.InvalidRequestError: Table 'tem' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
时间: 2023-11-22 20:54:48 浏览: 70
这个错误通常是因为在同一个 `MetaData` 实例中尝试定义两个同名的表。可以尝试在创建 `Table` 对象时,指定 `extend_existing=True` 参数来覆盖已有的表。
例如:
```
from sqlalchemy import Table, Column, Integer, MetaData
metadata = MetaData()
# 定义一个名为 tem 的表
tem_table = Table('tem', metadata,
Column('id', Integer, primary_key=True),
...
)
# 重复定义名为 tem 的表,使用 extend_existing=True 参数
tem_table = Table('tem', metadata,
Column('id', Integer, primary_key=True),
...
extend_existing=True
)
```
如果你已经创建了 `MetaData` 对象,可以在创建 `Table` 对象时,将其关联到现有的 `MetaData` 对象上。
例如:
```
metadata = MetaData()
# 表示将 tem_table 关联到 metadata 对象上
tem_table = Table('tem', metadata,
Column('id', Integer, primary_key=True),
...
)
# 在另一个文件中
from sqlalchemy import create_engine
engine = create_engine('sqlite:///mydatabase.db')
metadata = MetaData(bind=engine)
# 表示将 tem_table2 关联到 metadata 对象上,这里的 tem_table2 是与 tem_table 结构相同的另一个表
tem_table2 = Table('tem', metadata, extend_existing=True)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)