英文:
VSCode Intellisense for SQLAlchemy query result
问题
在第3行的代码中,customer.age = 50
,可能是因为在前一行中,VSCode无法推断first()
方法的返回类型,所以Intellisense不起作用。
您可以尝试显式地使用类转换或其他解决方法,以使类Customer
的方法和字段在Intellisense中对其对象customer
可用。
编辑1:
我已经像这样声明了类:
class Customer(base):
__tablename__ = "customers"
name = Column(String, primary_key=True, unique=True)
age = Column(Float, nullable=False)
在VSCode的Intellisense中,它仅显示Customer
的dunder字段。它不显示name
和age
字段。
编辑2:
在我的VSCode中,已安装了以下与Python相关的扩展:
- Pylance
- Python
- Python Environment Manager
- Python Extension Pack
- Python Indent
英文:
Below is a code fragment for querying a table using SQLAlchemy and reading the first record:
session = Session()
customer = session.query(Customer).filter(Customer.name == "John").first()
customer.age = 50
In the 3rd line of code customer.age = 50
, the VSCode Intellisense does not work, perhaps because VSCode cannot infer what will be the return type of first()
method in the previous line.
Is there a way I can use the class cast explicitly or any other workaround, so that the methods and fields of the class Customer
become available for its object customer
in Intellisense?
EDIT 1:
I have declared class like this:
class Customer(base):
__tablename__ = "customers"
name = Column(String, primary_key=True, unique=True)
age= Column(Float, nullable = False)
In the VSCode Intellisense, it is showing only the dunder fields of Customer
. It is not showing the name
and age
fields.
Below is the screenshot how it is coming for me in VSCode:
EDIT 2:
In my VSCode, the following Python related extensions are installed:
- Pylance
- Python
- Python Environment Manager
- Python Extension Pack
- Python Indent
答案1
得分: 1
尝试:
customer: Customer = session.query(Customer).filter(Customer.name == "John").first()
有了这个明确的类型提示,自动完成可能会起作用。
英文:
Try:
customer: Customer = session.query(Customer).filter(Customer.name == "John").first()
With this explicit type hint, the auto completion is likely to work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论