简体   繁体   English

为什么peewee包含'id'列进入mysql select查询?

[英]Why is peewee including the 'id' column into the mysql select query?

I am trying to learn how to use peewee with mysql. 我正在尝试学习如何使用myse的peewee。

I have an existing database on a mysql server with an existing table. 我在具有现有表的mysql服务器上有一个现有的数据库。 The table is currently empty (I am just testing right now). 该表目前是空的(我现在正在测试)。

>>> db = MySQLDatabase('nhl', user='root', passwd='blahblah')
>>> db.connect()


>>> class schedule(Model):
...     date = DateField()
...     team = CharField()
...     class Meta:
...             database = db

>>> test = schedule.select()
>>> test
<class '__main__.schedule'> SELECT t1.`id`, t1.`date`, t1.`team` FROM `nhl` AS t1 []
>>> test.get()

I get the following error: 我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/peewee.py", line 1408, in get
    return clone.execute().next()
  File "/usr/lib/python2.6/site-packages/peewee.py", line 1437, in execute
    self._qr = QueryResultWrapper(self.model_class, self._execute(), query_meta)
  File "/usr/lib/python2.6/site-packages/peewee.py", line 1232, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/usr/lib/python2.6/site-packages/peewee.py", line 1602, in execute_sql
    res = cursor.execute(sql, params or ())
  File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 't1.id' in 'field list'")

Why is peewee adding the 'id' column into the select query? 为什么peewee会在select查询中添加'id'列? I do not have an id column in the table that already exists in the database. 我没有在数据库中已存在的表中的id列。 I simply want to work with the existing table and not depend on peewee having to create one every time I want to interact with the database. 我只想使用现有的表,而不是每次我想与数据库交互时都要依赖peewee创建一个表。 This is where I believe the error is. 这是我认为错误的地方。

The result of the query should be empty since the table is empty but since I am learning I just wanted to try out the code. 查询的结果应该是空的,因为表是空的,但因为我正在学习,我只是想尝试代码。 I appreciate your help. 我感谢您的帮助。

EDIT 编辑

Based on the helpful responses by Wooble and Francis I come to wonder whether it even makes sense for me to use peewee or another ORM like sqlalchemy. 基于Wooble和Francis的有用回应,我开始怀疑使用peewee或其他ORM(如sqlalchemy)是否合理。 What are the benefits of using an ORM instead of just running direct queries in python using MySQLdb? 使用ORM而不是仅使用MySQLdb在python中运行直接查询有什么好处?

This is what I expect to be doing: 这是我期望做的事情:

-automatically downloading data from various web servers. - 自动从各种Web服务器下载数据。 Most of the data is in xls or csv format. 大多数数据采用xls或csv格式。 I can convert the xls into csv using the xlrd package. 我可以使用xlrd包将xls转换为csv。

-parsing/processing the data in list objects before inserting/bulk-inserting into a mysql db table. - 在插入/批量插入到mysql数据库表之前,对列表对象中的数据进行分析/处理。

-running complex queries to export data from mysql into python into appropriate data structured (lists for example) for various statistical computation that is easier to do in python instead of mysql. - 运行复杂的查询以将数据从mysql导出到python中,形成适当的数据结构(例如列表),用于各种统计计算,这在python而不是mysql中更容易实现。 Anything that can be done in mysql will be done there but I may run complex regressions in python. 可以在mysql中完成的任何事情都可以在那里完成,但我可能会在python中运行复杂的回归。

-run various graphical packages on the data retrieved from queries. - 从查询中检索的数据上运行各种图形包。 Some of this may include using the ggplot2 package (from R-project), which is an advanced graphical package. 其中一些可能包括使用ggplot2包(来自R-project),这是一个高级图形包。 So I will involve some R/Python integration. 所以我将涉及一些R / Python集成。

Given the above - is it best that I spend the hours hacking away to learn ORM/Peewee/SQLAlchemy or stick to direct mysql queries using MySQLdb? 鉴于上述情况 - 最好是花费时间去学习ORM / Peewee / SQLAlchemy或坚持使用MySQLdb直接进行mysql查询?

Most simple active-record pattern ORMs need an id column to track object identity. 大多数简单的活动记录模式ORM需要id列来跟踪对象标识。 PeeWee appears to be one of them (or at least I am not aware of any way to not use an id). PeeWee似乎是其中之一(或者至少我不知道任何使用id的方法)。 You probably can't use PeeWee without altering your tables. 您可能无法在不更改表格的情况下使用PeeWee。

Your existing table doesn't seem to be very well designed anyway, since it appears to lack a key or compound key. 您现有的表格似乎设计得不是很好,因为它似乎缺少键或复合键。 Every table should have a key attribute - otherwise it is impossible to distinguish one row from another. 每个表都应该有一个键属性 - 否则不可能将一行与另一行区分开来。

If one of these columns is a primary key, try adding a primary_key=True argument as explained in the docs concerning non-integer primary keys 如果其中一列是主键,请尝试添加primary_key=True参数,如有关非整数主键的文档中所述

date = DateField(primary_key=True)

If your primary key is not named id , then you must set your table's actual primary key to a type of "PrimaryKeyField()" in your peewee Model for that table. 如果您的主键未命名为id ,则必须将表的实际主键设置为该表的peewee Model中的“PrimaryKeyField()”类型。

You should investigate SQLAlchemy , which uses a data-mapper pattern. 您应该调查SQLAlchemy ,它使用数据映射器模式。 It's much more complicated, but also much more powerful. 它更复杂,但也更强大。 It doesn't place any restrictions on your SQL table design, and in fact it can automatically reflect your table structure and interrelationships in most cases. 它对您的SQL表设计没有任何限制,事实上它可以在大多数情况下自动反映您的表结构和相互关系。 (Maybe not as well in MySQL since foreign key relationships are not visible in the default table engine.) Most importantly for you, it can handle tables which lack a key. (在MySQL中可能不太好,因为在默认表引擎中看不到外键关系。)最重要的是,它可以处理缺少键的表。

If your primary key column name is other than 'id' you should add additional field to that table model class: 如果您的主键列名称不是“id”,则应向该表模型类添加其他字段:

class Table(BaseModel):
    id_field = PrimaryKeyField()

That will tell your script that your table has primary keys stored in the column named 'id_field' and that column is INT type with Auto Increment enabled. 这将告诉您的脚本您的表具有存储在名为“id_field”的列中的主键,并且该列是启用了自动增量的INT类型。 Here is the documentation describing field types in peewee. 是描述peewee中字段类型的文档。

If you want more control on your primary key field, as already pointed by Francis Avila, you should use primary_key=True argument when creating field: 如果你想要对主键字段进行更多控制,正如Francis Avila已经指出的那样,你应该在创建字段时使用primary_key = True参数:

class Table(BaseModel):
    id_field = CharField(primary_key=True)

See this link on non-integer primary keys documentation 请参阅非整数主键文档中的此链接

You have to provide a primary_key field for this model. 您必须为此模型提供primary_key字段。 If your table doesn't have a single primary_key field(just like mine), a CompositeKey defined in Meta will help. 如果你的表没有一个primary_key字段(就像我的一样),Meta中定义的CompositeKey会有所帮助。

primary_key = peewee.CompositeKey('date', 'team')

您需要使用peewee的create table方法来创建实际的数据库表,然后才能调用select() ,这将在表中创建一个id列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM