演说者ORM模型创建方法无效的SQL

时间:2018-10-05 20:14:31

标签: python orm relational-database

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

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" ([]))

看来,演说家只是没有在这里填写列和列。我还尝试了几种不同的句法将字典放入其中,使用** sample_customer并将其直接放入字典中,但都不起作用,所有方法都具有相同的例外。我开始通过从口述员库中打印出东西开始调试,但是还没到任何地方。

如果我单独进行模型属性分配并使用像这样的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()语法失败吗?

1 个答案:

答案 0 :(得分:0)

我认为答案是: 只需传递字典,而不是对属性使用关键字符号:

queue = []
for node in queue:
    if not node:
        continue
    for child in (node.left, node.right):
        queue.append(child)

Customer.create(sample_customer) 

哪些是有效的符号