小编qew*_*jhb的帖子

使用 AsyncEngine 执行任何 SQL 查询时出现 ObjectNotExecutableError

我正在使用 async_engine。当我尝试执行任何操作时:

async with self.async_engine.connect() as con:
    query = "SELECT id, name FROM item LIMIT 50;"
    result = await con.execute(f"{query}")
Run Code Online (Sandbox Code Playgroud)

我越来越:

Exception has occurred: ObjectNotExecutableError
Not an executable object: 'SELECT id, name FROM item LIMIT 50;'
Run Code Online (Sandbox Code Playgroud)

这个问题之前由用户@stilmaniac提出过,但现在已从SO 中删除

我在谷歌搜索缓存中找到了它,这里是副本

我有同样的问题,所以我重新询问它,但原始版本如下:


我正在尝试从元数据创建表,如下所示:

Base = declarative_base()

properties = Table(
    'properties', Base.metadata,
    # ...
    Column('geolocation', Geography(geometry_type='POINT', srid=4326)),
    # ... 
)

engine = create_async_engine("postgresql+asyncpg://user:password@postgres/")
async with engine.begin() as conn:
    await conn.run_sync(Base.metadata.create_all)
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: 'CREATE INDEX …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy python-3.x python-asyncio asyncpg

47
推荐指数
2
解决办法
7万
查看次数

如何在FastAPI响应模式中使用箭头类型?

我想使用Arrow类型作为FastAPI响应,因为我已经在SQLAlchemy模型中使用它(感谢sqlalchemy_utils)。

我准备了一个小型的独立示例,其中包含一个最小的 FastAPI 应用程序。我希望这个应用程序product1从数据库返回数据。

不幸的是,下面的代码给出了异常:

Exception has occurred: FastAPIError
Invalid args for response field! Hint: check that <class 'arrow.arrow.Arrow'> is a valid pydantic field type
Run Code Online (Sandbox Code Playgroud)
Exception has occurred: FastAPIError
Invalid args for response field! Hint: check that <class 'arrow.arrow.Arrow'> is a valid pydantic field type
Run Code Online (Sandbox Code Playgroud)

要求.txt:

sqlalchemy==1.4.23
sqlalchemy_utils==0.37.8
arrow==1.1.1
fastapi==0.68.1
uvicorn==0.15.0
Run Code Online (Sandbox Code Playgroud)

这个错误已经在那些 FastAPI 问题中讨论过:

  1. https://github.com/tiangolo/fastapi/issues/1186
  2. https://github.com/tiangolo/fastapi/issues/2382

一种可能的解决方法是添加此代码(源代码):

import sqlalchemy
import uvicorn
from arrow import Arrow
from fastapi import …
Run Code Online (Sandbox Code Playgroud)

arrow-python openapi pydantic fastapi

7
推荐指数
1
解决办法
2510
查看次数

SQLAlchemy:如何引用只读表映射?

对于 SQLAlchemy,我想使用第一个表,其中包含可用作第二个表中字段的合法值的值的记录/字段。

但是,第一个表的记录不应该能够从第二个表的字段中更改,只能被引用。

我在 Sqlalchemy 中有一个包含两个模型的数据库:

class Country(Base):
__tablename__ = 'countries'

id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
code = Column(String, unique=True)


class Address(Base):
__tablename__ = 'addresses'

id = Column(Integer, primary_key=True)
row_1 = Column(String)
row_2 = Column(String)
row_3 = Column(String)
row_4 = Column(String)
row_5 = Column(String)
country_id = Column(Integer, ForeignKey('countries.id'))
country = relationship("Country")
Run Code Online (Sandbox Code Playgroud)

我使用国家/地区模型将世界各国的名称和代码填充到数据库中。(维基百科

现在我实例化一个地址对象 ,a1c1为其分配一个国家/地区对象:

a1.country = c1
Run Code Online (Sandbox Code Playgroud)

这有效;现在我可以这样做:

a1.country.name
>>> 'Iceland'
Run Code Online (Sandbox Code Playgroud)

不过,我也可以这样做:

a1.country.name = 'Switzerland'
a1.country.name
>>> 'Switzerland'
Run Code Online (Sandbox Code Playgroud)

这不是我想要的。我希望能够使用数据库中的国家/地区来分配地址对象,并从地址对象中引用国家/地区,但是只能通过直接访问国家/地区对象来更改国家/地区对象,例如一个孤立的国家/地区对象是对数据库的查询结果。

我想使用国家/地区映射作为只读引用,其中包含地址国家/地区列的允许值。

我怎样才能做到这一点? …

python sqlalchemy

5
推荐指数
0
解决办法
3543
查看次数

如何通过“Clash for Windows”路由互联网流量(来自 Python 代码的 Ping 不起作用)

from os import system
system("ping www.twitter.com")
system("ping www.yahoo.com")
system("ping www.facebook.com")
Run Code Online (Sandbox Code Playgroud)

我在中国,这里禁止推特和脸书。我可以使用Clash for Windows软件在浏览器中打开它们。

我必须从 Twitter 下载推文。所以我需要使用 Python ping 网站来获取推文。虽然我无法ping通网站。

如何让我的 Python 代码使用Clash for Windows.

上面代码的输出:

Pinging www.twitter.com [108.160.169.186] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 108.160.169.186:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

Pinging new-fp-shed.wg1.b.yahoo.com [180.222.102.201] with 32 bytes of data:
Reply from 180.222.102.201: bytes=32 time=258ms TTL=42
Reply …
Run Code Online (Sandbox Code Playgroud)

python windows vpn ping

5
推荐指数
1
解决办法
398
查看次数

如何使用“fixture”和“parametrize”为pytest测试设置环境变量

我有 pytest 测试,其结果可能取决于环境变量。我想测试它们的这个环境变量的多个值。

我只想有一个设置此环境变量的固定装置,但我希望能够为每个测试而不是每个固定装置配置这些值。

我该怎么做?

python environment-variables pytest python-3.x parametrized-testing

4
推荐指数
1
解决办法
5617
查看次数

如何启动另一个程序并在Python脚本完成后保持它运行?

我的问题类似于How to run Python's subprocess and left it in back,但是那里列出的答案都不适合我。

我尝试运行一个程序,例如 Slack 或 Discord(或问题更新中列出的其他程序)。即使我的脚本完成,我也希望程序能够运行。

我需要这个才能在 Windows 上工作。

注意:仅当 Slack / Discord 从 Python 脚本启动时才会出现此问题,如果之前运行过,则不会关闭。

示例代码:(如您所见,我尝试了多种方法):

import os, subprocess
from time import sleep
from subprocess import Popen, PIPE, STDOUT

# discord_path=r"C:\Program Files\Discord\Discord.exe"
# discord_path2=r"C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Discord Inc\Discord.lnk"
# os.startfile(discord_path2)

# subprocess.run([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"],shell=True)
# subprocess.Popen([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"],shell=True)
# subprocess.call([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"])
# subprocess.Popen([r"C:\Users\user\AppData\Local\Discord\Update.exe", "--processStart", "Discord.exe"], stdin=None, stdout=None, stderr=None, close_fds=True)

# slack_path2=r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Slack Technologies Inc\Slack.lnk"
# os.startfile(slack_path2)
#  stdin=None, stdout=None, stderr=None, …
Run Code Online (Sandbox Code Playgroud)

python windows subprocess background-process python-3.x

3
推荐指数
1
解决办法
3108
查看次数

如何通过 CLI 获取 Google Cloud SQL 实例的 IP

我在 Google Cloud Platform 上使用 Cloud SQL。

将数据库的IP作为变量很有用,因此可以在脚本中使用它。

可以使用什么命令来实现这一点?

重击示例:

export DP_IP=$(gcloud ................)
Run Code Online (Sandbox Code Playgroud)

cloud ip command-line-interface google-cloud-sql google-cloud-platform

2
推荐指数
1
解决办法
1702
查看次数