gpt4 book ai didi

python - Orator ORM模型创建方法无效SQL

转载 作者:行者123 更新时间:2023-12-01 08:58:00 25 4
gpt4 key购买 nike

我有一个通过迁移创建的数据库。我的一张 table 看起来像这样

def create_customer_table(self):
with self.schema.create("customer") as table:
table.char("name",120).unique()
table.integer("transmitting_hours").default(24) #how many hours after transmission vehicle is considered transmitting
table.boolean("is_tpms").default(False)
table.boolean("is_dor").default(False)
table.boolean("is_otr").default(False)
table.boolean("is_track_and_trace").default(False)
table.char("contact_person",25)
table.char("created_by",25)
table.enum("temperature_unit",TEMP_UNITS)
table.enum("pressure_unit",PRESSURE_UNITS)
table.enum("distance_unit",DISTANCE_UNITS)
table.char("time_zone",25)
table.char("language",2)
table.timestamps()

我上面有一个非常简单的 ORM 模型

class Customer(Model):
__table__ = "customer"
__timestamps__ = False
__primary_key__ = "name"
__fillable__ = ['*']

然后我尝试使用以下代码进行基本插入

def add_sample_customer():
sample_customer = {}
sample_customer["name"] = "customer_2"
sample_customer["contact_person"] = "Abradolf"
sample_customer["created_by"] = "Frodo"
sample_customer["time_zone"] = "GMT-5"
sample_customer["language"] = "EN"
sample_customer["temperature_unit"] = "FAHRENHEIT"
sample_customer["pressure_unit"] = "PSI"
sample_customer["distance_unit"] = "MI"
customer_model = Customer.create(_attributes = sample_customer)

我从这段代码中得到的异常看起来像

orator.exceptions.query.QueryException: syntax error at or near ")"
LINE 1: INSERT INTO "customer" () VALUES () RETURNING "name"
(SQL: INSERT INTO "customer" () VALUES () RETURNING "name" ([]))

看起来演说家没有填写这里的 cols 和 vals。我还尝试了几种不同的语法方法,使用 **sample_customer 将字典放入其中,也只是直接将字典放入其中,但它们都不起作用,但都有相同的异常(exception)。我开始通过从演讲者库中打印内容来进行调试,但还没有取得任何进展。

如果我单独进行模型属性分配并使用这样的 model.save() 方法,我的插入就可以工作

def add_sample_customer():
sample_customer = {}
sample_customer["name"] = "customer_2"
sample_customer["contact_person"] = "Abradolf"
sample_customer["created_by"] = "Frodo"
sample_customer["time_zone"] = "GMT-5"
sample_customer["language"] = "EN"
sample_customer["temperature_unit"] = "FAHRENHEIT"
sample_customer["pressure_unit"] = "PSI"
sample_customer["distance_unit"] = "MI"
customer_model = Customer()
for k,v in sample_customer.items():
setattr(customer_model,k,v)
customer_model.save()

有人理解为什么 model.create() 语法失败吗?

最佳答案

我认为答案是:简单地传递字典,而不是使用带有属性的关键字表示法:

Customer.create(sample_customer) 

Customer.create(attribute=value,attribute2=value2,..etc)

哪些是有效的符号

关于python - Orator ORM模型创建方法无效SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52672731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com