【发布时间】:2018-05-28 08:59:59
【问题描述】:
使用 PeeWee Orm 我创建了两个表(我已经删除了很多与此查询无关的字段)
产品表
class Product(BaseModel):
fm_price = FloatField(null=True)
magento = IntegerField(db_column='magento_id', null=True, index=True)
sku = CharField(index=True, null=True, unique=True)
# Compare these two fields to on or offline products
magento_status_fm = IntegerField(null=True)
magento_status_ff = IntegerField(null=True)
# Date fields to put error dates in
ff_price_error = DateField(index=True, null=True)
ff_stock_error = DateField(index=True, null=True)
fm_price_error = DateField(index=True, null=True)
fm_stock_error = DateField(index=True, null=True)
ff_check_date = DateField(null=True, index=True) # When did we get this product from FF
class Meta:
db_table = 'products'
indexes = (
(('fm_price', 'vip_price', 'education_price'), False),
)
价格表
class Price(BaseModel):
price_pk = PrimaryKeyField()
entity_id = ForeignKeyField(Product, to_field='magento', null=True, on_update='CASCADE', on_delete='CASCADE')
customer_group_id = IntegerField(null=True)
final_price = FloatField(null=True)
min_price = FloatField(null=True)
check_date = DateField(null=True)
class Meta:
indexes = (
(('entity_id', 'customer_group_id'), True),
)
示例内容:
产品:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------
| id | CREATION | MODIFICATION | fm_price | fm_stock | vip_price | education_price | magento_id | omit | sku | url | web_stock | magento_status_fm | magento_status_ff | ff_price_error | ff_stock_error | fm_price_error | fm_stock_error | fm_vip_price_error | ff_vip_price_error | ff_check_date |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------
| 3 | 2017-12-12 12:53:01 | 2017-12-14 09:31:44 | 109 | NULL | NULL | NULL | 15212 | NULL | epiea10nach1 | https://www.absolut… | NULL | NULL | 1 | NULL | NULL | NULL | NULL | NULL | NULL | 2017-12-14 |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------
| 5 | 2017-12-12 12:53:01 | 2017-12-14 09:31:44 | 545 | NULL | NULL | NULL | 15433 | NULL | yamapx700mk2blk | https://www.absolut… | NULL | NULL | 1 | NULL | NULL | NULL | NULL | NULL | NULL | 2017-12-14 |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------
价格:
---------------------------------------------------------------------------------------------------------------------------
| price_pk | CREATION | MODIFICATION | entity_id_id | customer_group_id | final_price | min_price | check_date |
---------------------------------------------------------------------------------------------------------------------------
| 260 | 2017-12-12 12:53:39 | NULL | 15212 | 0 | 109 | 109 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 261 | 2017-12-12 12:53:39 | NULL | 15212 | 1 | 109 | 109 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 262 | 2017-12-12 12:53:39 | NULL | 15212 | 2 | 109 | 109 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 263 | 2017-12-12 12:53:39 | NULL | 15212 | 3 | 109 | 109 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 264 | 2017-12-12 12:53:39 | NULL | 15212 | 4 | 109 | 109 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 265 | 2017-12-12 12:53:39 | NULL | 15212 | 5 | 109 | 109 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 266 | 2017-12-12 12:53:39 | NULL | 15212 | 6 | 109 | 109 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 988 | 2017-12-12 12:53:42 | NULL | 15431 | 0 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 989 | 2017-12-12 12:53:42 | NULL | 15431 | 1 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 990 | 2017-12-12 12:53:42 | NULL | 15431 | 2 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 991 | 2017-12-12 12:53:42 | NULL | 15431 | 3 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 992 | 2017-12-12 12:53:42 | NULL | 15431 | 4 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 993 | 2017-12-12 12:53:42 | NULL | 15431 | 5 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 994 | 2017-12-12 12:53:42 | NULL | 15431 | 6 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 1002 | 2017-12-12 12:53:42 | NULL | 15433 | 0 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 1003 | 2017-12-12 12:53:42 | NULL | 15433 | 1 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 1004 | 2017-12-12 12:53:42 | NULL | 15433 | 2 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 1005 | 2017-12-12 12:53:42 | NULL | 15433 | 3 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 1006 | 2017-12-12 12:53:42 | NULL | 15433 | 4 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 1007 | 2017-12-12 12:53:42 | NULL | 15433 | 5 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
| 1008 | 2017-12-12 12:53:42 | NULL | 15433 | 6 | 545 | 545 | NULL |
---------------------------------------------------------------------------------------------------------------------------
这些在 Magento_Id (magento) 字段上连接在一起,该字段是一个整数。 产品表中有我们正确的价格 价格表包含网站上针对不同客户群的 magento 价格
每个产品可以有很多价格,我希望根据我们的内部数据库检查价格是否正确(因此网站是正确的)
我编写了一个 SQL 查询,显示价格表中的价格与 Product 表中的价格不相等的位置。
我也在 PeeWee Orm 中得到了这个工作。
然而,我正在努力让 peewee 向我展示 Price 表中的 final_price,即使我的 SQL 查询使用它作为比较器。
示例 SQL:
SELECT
sku, magento_id, fm_price, price.final_price, `url`
FROM
products
JOIN
price
ON
`products`.`magento_id` = `price`.`entity_id_id` AND `price`.`customer_group_id` = 0
WHERE
`products`.`fm_price` != `price`.`final_price`
示例输出:
---------------------------------------------------------------------------------------------------------------
| sku | magento_id | fm_price | final_price | url |
---------------------------------------------------------------------------------------------------------------
| bsrhtstudio20h | 17581 | 399 | 449 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| rolac60rw | 17697 | 479 | 475 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| yamthr5 | 17833 | 169.85 | 169 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| rolgk3b | 19636 | 155 | 152 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| rolgk3 | 19763 | 155 | 152 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| bosac3 | 20115 | 113 | 107.15 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| bosbd2 | 20128 | 79 | 77.95 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
| bosch1 | 20140 | 77.5 | 71.92 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
---------------------------------------------------------------------------------------------------------------
还有我的 Peewee 查询
price_errors = Product.select(Product, Price.final_price).join(Price, on=(Product.magento == Price.entity_id) & (
Price.customer_group_id == 0)).where(Product.fm_price != Price.final_price)
这给了我正确的结果,但我无法访问要显示的 Price.final_price
我尝试了以下方法:
对于 price_errors 中的 price_error: 打印(price_error.price.final_price)
给出:
AttributeError: 'Product' object has no attribute 'price'
我可以访问price_set,但这显示了所有通过外键连接相关的项目,而不是上面指定的连接
我已经在 join 和 select 中添加了别名,但仍然无法得到任何东西。
如果我用
执行它price_errors = Product.raw("""SELECT id, sku, magento_id, fm_price,
price.final_price, `url`
FROM products
JOIN price
ON `products`.`magento_id` =
`price`.`entity_id_id` AND `price`.`customer_group_id` = 0
WHERE `products`.`fm_price` !=
`price`.`final_price`""")
然后我可以访问 price_error.final_price
但是这种方式破坏了 orm 的使用 :(
【问题讨论】: