sqlalchemy postgresql,其中int =字符串 [英] sqlalchemy postgresql where int = string

查看:122
本文介绍了sqlalchemy postgresql,其中int =字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Postgresql有0年的经验,并且正在将使用sqlalchemy用python编写的应用程序部署到具有postgres的服务器上。



为了开发,我使用了sqlite服务器。 / p>

事情进展得很顺利,但是我遇到了一个障碍,我不知道该怎么解决。



我有三个看起来像这样的表

  class Car(db.Model):
id = db.Column(db .Integer,primary_key = True)
...

class Truck(db.Model):
id = db.Column(db.String(32),primary_key = True )
...

类Vehicles(db.Model):
id = db.Column(db.Integer,primary_key = True)
type = db。 Column(db.String)#这是'car'或'truck'
value = db.Column(db.String)#这是汽车或卡车的ID
...

我有一个查询,该查询从辆车中选择,其中 type ='car'AND value = 10
这将引发错误:
sqlalchemy.exc.Pro grammingError:(ProgrammingError)运算符不存在:整数=字符变化



所以我想这是因为 Car。 id 是一个整数, Vehicle.value 是一个字符串。.



如何在sqlalchemy中写这个查询?有没有办法编写它并使它与我的sqlite开发环境和pgsql生产兼容?



当前看起来像这样



db.session.query(Vehicle).filter(Car.id == Vehicle.value)



PS:卡车ID必须为字符串,汽车ID必须为int。我对此没有控制权。

解决方案

简单地转换为字符串:

  db.session.query(Vehicle).filter(str(Car.id)== Vehicle.value)


如果 Car.id 是一个局部整数,则为整数。



如果需要在联接中使用它,请数据库将其强制转换为字符串:

 从sqlalchemy.sql.expression导入演员表

db.session.query(Vehicle).filter(cast(Car.id,sqlalchemy.String)== Vehicle.value)

如果另一列中的字符串值包含数字并可能包含空格,则可能需要考虑修剪或而是将字符串值转换为整数(并将整数列保留为整数)。


I have 0 experience with postgresql and am deploying an app written in python using sqlalchemy to a server with postgres.

For development, I used an sqlite server.

Things are going pretty smoothly, but I hit a bump I don't know how to resolve.

I have three tables that look like that

class Car(db.Model):
     id= db.Column(db.Integer, primary_key=True)
     ...

class Truck(db.Model):
     id= db.Column(db.String(32), primary_key=True)
     ...

class Vehicles(db.Model):
     id= db.Column(db.Integer, primary_key=True)
     type= db.Column(db.String) #This is either 'car' or 'truck'
     value= db.Column(db.String) #That's the car or truck id
     ...

I have a query that selects from Vehicles where type = 'car' AND value = 10 This is throwing an error: sqlalchemy.exc.ProgrammingError: (ProgrammingError) operator does not exist: integer = character varying

So I guess this is because Car.id is an int and Vehicle.value is a string..

How to write this query in sqlalchemy? Is there a way to write it and make it compatible with my sqlite dev environment and the pgsql production?

currently it looks like that

db.session.query(Vehicle).filter(Car.id == Vehicle.value)

PS: The truck id has to be a string and the car id has to be an int. I don't have control over that.

解决方案

Simply cast to a string:

db.session.query(Vehicle).filter(str(Car.id) == Vehicle.value)

if Car.id is a local variable that is an int.

If you need to use this in a join, have the database cast it to a string:

from sqlalchemy.sql.expression import cast

db.session.query(Vehicle).filter(cast(Car.id, sqlalchemy.String) == Vehicle.value)

If the string value in the other column contains digits and possibly whitespace you may have to consider trimming, or instead casting the string value to an integer (and leave the integer column an integer).

这篇关于sqlalchemy postgresql,其中int =字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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