akumoth/peewee-issues huggingface dataset:
Issue: Implicit add Primary Key for id field
Author: horpto
State: closed
Created: 2015-06-06 19:26:07

```
class Model(BaseModel):
    id = TextField()
    field = TextField()
```

Field id implicitly is primary key now. TextField, for example, is not really suitable to be a primary key, but unexpectedly it is. Maybe this situation need report warning or even error.


This is just the standard peewee behavior. If you want a different field to be primary key, declare a different one.
Issue: Support PostgreSql table inheritance
Author: horpto
State: closed
Created: 2015-06-04 23:44:26

PostgreSql has cool feature like [Table inheritance](http://www.postgresql.org/docs/9.0/static/ddl-inherit.html). It'd be good to implement this feature using inheritance field in Meta class of model.


I don't know very much about that particular feature, so I can't really say that I have any plans on implementing it. I think I will defer on this one, but if you'd like to discuss making a pull-request let me know.
Issue: Support set timezone
Author: rodumani
State: closed
Created: 2015-06-04 05:15:29

Hello!

I'm making query like following which filter TIMESTAMP field between two dates.
However, the problem is that our MySQL's default timezone is UTC and I want to query in KST timezone.

``` python
Log
  .select(fn.Sum(Log.cnt))
  .where(Log.insert_dt.between(start_date, end_date))
  .scalar()
```

When using SQL, I can set timezone by `set timezone="+09:00"`. I cannot find option to set timezone on Peewee.


Just call `db.execute_sql('set timezone="+09:00"')`. You can also check your mysql driver's docs to see if it accepts a `time_zone` parameter. If not, you can subclass `MySQLDatabase`, override the `_connect` method, and run `set time_zone` on the newly-created connection.
Issue: Aggregate Multiple Joins to Same Table
Author: arrowgamer
State: closed
Created: 2015-06-02 01:07:18

I don't appear able to load a join via `aggregate_rows()` when multiple foreign keys point to the same object.  This used to be possible.

Consider the below:

``` python
from peewee import *
from unittest import TestCase

db = SqliteDatabase(':memory:')


class Thing(Model):
    name = CharField()

    class Meta:
        database = db


class SomethingWithTwoDifferentThings(Model):
    name = CharField()
    creator = ForeignKeyField(Thing, related_name='creations')
    owner = ForeignKeyField(Thing, related_name='owned_stuff')

    class Meta:
        database = db


class EagerLoadingIsHard(TestCase):
    @classmethod
    def setUpClass(cls):
        SomethingWithTwoDifferentThings.create_table()
        Thing.create_table()
        thing1 = Thing.create(name='Thing 1')
        thing2 = Thing.create(name='Thing 2')
        SomethingWithTwoDifferentThings.create(name='Something', owner=thing1, creator=thing2)

    def test_load_my_stuff_please(self):
        something = (SomethingWithTwoDifferentThings
                     .select(SomethingWithTwoDifferentThings, Thing)
                     .join(Thing, on=(SomethingWithTwoDifferentThings.owner == Thing.id).alias('owner'))
                     .where(SomethingWithTwoDifferentThings.name == 'Something')
                     .aggregate_rows())[0]

        self.assertIn('owner', something._obj_cache)
        self.assertEqual(something._obj_cache['owner'].name, 'Thing 1')
```

I'm able to load the creator via simple `.join(Thing)`, though there doesn't appear to be a way to load the owner.


I didn't realize this broke with the latest changes, that sucks should've had a unit test in place to catch it. I'll look into it, but I'm thinking maybe I need to just scrap `AggregateQueryResultWrapper` and try a rewrite with some new ideas, it's becoming a bit of an eye-sore.


Did you try `on=SomethingWithTwoDifferentThings.owner`, i.e. passing just the field object rather than using a boolean expression?


Yeah, that's what I had initially, I put what I did above because I figured it was more implicit. Both ways yield the same error:

```
Error
Traceback (most recent call last):
  File "/Users/arrow/dev/test/peewee_test.py", line 37, in test_load_my_stuff_please
    .aggregate_rows())[0]
  File "/Users/arrow/dev/test/venv/lib/python2.7/site-packages/peewee.py", line 2703, in __getitem__
    res.fill_cache(index)
  File "/Users/arrow/dev/test/venv/lib/python2.7/site-packages/peewee.py", line 1912, in fill_cache
    self.next()
  File "/Users/arrow/dev/test/venv/lib/python2.7/site-packages/peewee.py", line 1898, in next
    obj = self.iterate()
  File "/Users/arrow/dev/test/venv/lib/python2.7/site-packages/peewee.py", line 2197, in iterate
    instance._data[metadata.src_fk.name]]
KeyError: 2
```


I believe this one should now be fixed in master.
Issue: Allow for using PEP8 compliant comparisons in boolean Fields
Author: ezk84
State: closed
Created: 2015-05-30 21:33:12

Current implementation necessitates this sort of syntax:

``` python
     Model.selec().where(Model.field == True)
```

This raises a `E712 comparison to True should be 'if cond is True:' or 'if cond:'` and similarly with comparisons to `False`. 

Would be nice to be able to do:

``` python
         Model.selec().where(Model.field is True)
```

or even perhaps:

``` python
         Model.selec().where(not Model.deleted)
```


There is no way to override the `is` lookup, so this won't work. Same thing for `not`, `and` and `or`.


@coleifer Would adding something like `.equals(value)` or `.bool(value)` (to http://docs.peewee-orm.com/en/latest/peewee/querying.html#query-operators) be out of the question?

I came across this issue because `autopep8` was breaking my queries to be pep8 compliant. For instance, my original query

```python
profile_image = Image.select().where(
    (Image.organization == org_id) &
    (Image.user == None) &
    (Image.id == image_id)
).get()
```
was having its `== None` replaced with `is None`, becoming

```python
profile_image = Image.select().where(
    (Image.organization == org_id) &
    (Image.user is None) &
    (Image.id == image_id)
).get()
```
which led to a `peewee.DoesNotExist` error. I'm not sure if something like @tuukkamustonen's suggestion was ever implemented (I didn't find it), but <strike> an alternative would be to define a constant like


```python
peewee.NULL = None
```
so comparison to null values can be compared with `==` in a `pep8` compliant way.

For instance,

```python
profile_image = Image.select().where(
    (Image.organization == org_id) &
    (Image.user == peewee.NULL) &
    (Image.id == image_id)
).get()
```

</strike>

**Update:** use @coleifer's solution below. 










To do null testing, use:

```python
Image.user.is_null(True)
```

Or

```python
Image.user.is_null(False)  # IS NOT NULL
```

Which is important because `IS NOT NULL` has different semantics than `NOT (... IS NULL)`.
Issue: Allow the use of connection pooling from within database url
Author: kylef
State: closed
Created: 2015-05-27 23:35:25

It would be nice if the [database URL](http://peewee.readthedocs.org/en/latest/peewee/playhouse.html#db-url) API would support connection pooling.

For example, an argument to the connect function which allows you to use pooling if available. `connect('uri', use_pooling=True)`


Well, actually if you look at the docs, this is now supported. I don't believe I've pushed those changes to PyPI yet, but they will be included in the next release.

![s1432770417 64](https://cloud.githubusercontent.com/assets/119974/7849927/bca1baf6-04a0-11e5-8a4c-4e017584592c.png)


Oh nice @coleifer, making this issue has been on my todo list for months since you asked me to on IRC.
Issue: Added id accessor to foreign relations, similar to Django.
Author: tals
State: closed
Created: 2015-05-23 03:28:30

Models with a ForeignKeyField instance now receive an additional attribute - attr_name_id. This allows you to access the foreign id without bringing in the entire model.

It's currently missing tests, but I figured I should see if you're interested in this first :)


Well, I originally didn't want to add this, but thinking about it, enough people have requested this that maybe it'd be a good idea. Thanks I'll go ahead and add some tests.
Issue: Getting just the ID of a foreign key requires full object
Author: tals
State: closed
Created: 2015-05-23 02:19:04

Hey there,

For illustrative purposes, lets use the following model:

``` python
class User(Model):
    name = CharField(max_length=50)

class Post(Model):
    author = ForeignKeyField(User)
    text = TextField()

all_posts_with_user = Post.select(Post, User).join(User).execute()
all_posts = Post.select().execute()
```

The issue I am seeing is that on `all_posts`, Peewee will do an eager lookup in the database as soon as `__getattr__` is hit on the author field's `RelationDescriptor`, which is unnecessary when you just want to work with the related object's id (like when doing any sort of client-side aggregate).

Django solves this by having an additional attribute of `modelname_id`. In our case, `Post` will have a `author_id` attribute in addition to the `author` attribute.

Would be nice to having something simiar in Peewee :)


Poking around the code, it's possible to get something like the Django flavor by having a new descriptor that just returns `_data[self.att_name]` and add it to the object during the `_new_()` call of `ForeignKeyField`


This query will join `Post` and `User` and eagerly fetch the post's associated user object.

``` python
all_posts_with_user = Post.select(Post, User).join(User).execute()
```

The other query you shared below will _not_ eagerly fetch the user, and will trigger a query when you attempt to access the `post.user` attribute.

``` python
all_posts = Post.select().execute()
```

As you noted in your second comment, you can retrieve the related object's ID by looking at `post._data['user']`. Rather than modifying the code to add a new attribute, you can either subclass `ForeignKeyField` with your own implementation or write a `property` on your model class that will lookup the ID, e.g.

``` python
class Post(Model):
    # ... fields ...
    @property
    def author_id(self):
        return self._data['author']
```


Yeah, eagerly was the wrong word to use - I meant that it fetched the entire object before it had to :)

Anyway, thanks for merging it in! :)
Issue: Request for support for complex/null operators
Author: johnboy2
State: closed
Created: 2015-05-15 23:11:32

As requested in the online documentation, I am proposing a feature change for peewee, as described below.

While looking at subclassing peewee.Database for a project, I quickly realized that some operations that work on PostgreSQL or SQLite have no _direct_ analogy on some platforms I'm looking to use it with. Usually, that has been because peewee expects comparison clauses to always be expressed using _in-fix_ semantics (eg _&lt;left_operand&gt; &lt;operator&gt; &lt;right_operand&gt;_), which isn't always possible.

Similarly, if a given operation cannot be supported at all (eg regex matching), there is no way to _forbid_ use of the corresponding peewee expressions. I could let those operations fail in the database (leading to troubleshooting based on DBMS error text), but that's nowhere near as useful as would be an exception raised by peewee containing a clearly-worded message.

To address these, I want to modify `QueryCompiler()._parse_expression()` so that _overridden_ operators don't necessarily have to be strings, but can also be a callable or `None`. Use of `None` would indicate an unsupported operator whose use raises an exception, while a callable would be expected to assemble and return whatever complex clause is needed to get the job done.

Here is an example that would become possible with the proposed change:

``` python
class MyCustomDB(peewee.Database):
    op_overrides = {
        peewee.OP.REGEXP: None,  # Raise ValueError if somebody tries this
        peewee.OP.LIKE:   lambda l, r: '%s LIKE %s COLLATE Latin1_General_BIN' % (l, r),
        peewee.OP.ILIKE:  'LIKE',
        peewee.OP.XOR:    lambda l, r: '((%s AND NOT %s) OR (NOT %s AND %s))' % (l, r, l, r),
    }
    # ...
```

Thoughts?


Hmm...while I can see the benefit of allowing operations to map to callables, I do not have plans to add that currently. You can always subclass `QueryCompiler` and provide your own implementation for `_parse_expression` that does the right thing.
Issue: FTSModels for sqlite pass `options` that are not expected.
Author: mklauber
State: closed
Created: 2015-05-12 20:41:47

I'm having an issue with using the sqlite_ext FTS extension.  I'm trying to follow the example from the documentation (reproduced below), but I'm getting the following stack trace.  (reproduced _further_ below).  Am I missing something, or is this part of the general experimental nature of the sqlite_ext?

``` python
class Metadata(Model):
    document  = ForeignKeyField(Document)
    field = CharField(index=True)
    content = TextField()

    class Meta:
        database = db

class MetadataIndex(FTSModel):
    value = TextField()

    class Meta:
        database = db


Metadata.create_table()
MetadataIndex.create_table(value=Metadata.content)
```

``` python
Traceback (most recent call last):
  File "models.py", line 43, in <module>
    MetadataIndex.create_table()
  File "/home/mklauber/.virtualenvs/metalogos/local/lib/python2.7/site-packages/playhouse/sqlite_ext.py", line 107, in create_table
    cls._meta.database.create_table(cls, options=options)
TypeError: create_table() got an unexpected keyword argument 'options'
```


Doh, Found it in the closed issues.  Sorry.
Issue: will peewee support table partition?
Author: pyloque
State: closed
Created: 2015-05-12 02:53:36

in our project, table_name is decided by entity field value, so different entities of the same model class can be saved to different tables with same table_name prefix.
I currently self maked an orm framework with less functions.I wish peewee can support table partition,so I could replace it with powerfull peewee.

```
class Account(Model):
    __partition_key__ = 'username'
    __partition_num__ = 256

    username = StringField()
    password = StringField()

Account.query_for('username_xxx')
Account.query_for_partition(123)
```


You can probably build something _on top_ of peewee, but peewee itself does not support table partitioning.

If you want to change the table your query uses at run-time, you can set the `ModelClass._meta.db_table` attribute, e.g.

``` python
Account._meta.db_table = 'account_123'
query = Account.select().where(Account.username == 'username_xxx')
Account._meta.db_table = 'account'
```

You could write a context manager, for example:

``` python
with Account.query_partition(123):
    query = Account.select().where(...)
```


One potential solution that we're looking at: http://architect.readthedocs.io/features/partition/index.html
Issue: Add method/hook for default table names
Author: mikemill
State: closed
Created: 2015-05-10 13:15:53

In our project we are using the singular form for the model names and the plural form for the tables.  This means each of our models has to have `class Meta: db_table = 'foos'`.  It'd be nice if there was a method or hook for defining the default table name algorithm that way it can be defined once.  Currently it is coded directly into `BaseModel.__new__`.


Nice idea, be happy to add that.


Wow, thanks for such a quick response.  That solution is awesome!
Issue: Ordering of set in back reference from ForeignKeyField
Author: josefdlange
State: closed
Created: 2015-05-09 23:05:39

Is it possible, in a simple way, to declare what order in which the back reference created by a ForeignKeyField returns? I can sort the items once I get them (in most cases since the backref groups shouldn't be huge), but offloading this work to the DB would be nice. Is there something right in front of me I am missing?

Thanks!


Well, let's say you have `User` and `Tweet` from the docs:

``` python
class User(Model):
    username = CharField(unique=True)

class Tweet(Model):
    user = ForeignKeyField(User, related_name='tweets')
    content = TextField()
    timestamp = DateTimeField(default=datetime.datetime.now)
```

You can observe that the `user.tweets` object is a `SelectQuery`:

``` python
>>> u = User.create(username='charlie')
>>> u.tweets
<class '__main__.Tweet'> SELECT "t1"."id", "t1"."user_id", "t1"."content", "t1"."timestamp" FROM "tweet" AS t1 WHERE ("t1"."user_id" = ?) [1]
```

Since it's just a query you can call `order_by()` on it:

``` python
>>> u.tweets.order_by(Tweet.timestamp.desc())
<class '__main__.Tweet'> SELECT "t1"."id", "t1"."user_id", "t1"."content", "t1"."timestamp" FROM "tweet" AS t1 WHERE ("t1"."user_id" = ?) ORDER BY "t1"."timestamp" DESC [1]
```

If you don't want to type this all the time, just create a `property` object on `User`:

``` python
class User(Model):
    # ... fields ...

    @property
    def tweets_ordered(self):
        return self.tweets.order_by(Tweet.timestamp.desc())
```

Then use `tweets_ordered`.


The property solution is slick. Thanks!


@coleifer then how do you call `tweets_ordered`? since it would belong to User?
It must be `u.tweets_ordered` , but `tweets_ordered` is a property of Tweet?

I had made a mistake, that should have read class User. I've edited the comment. Hope that helps.
Issue: KeyError with user-defined operations
Author: coleifer
State: closed
Created: 2015-05-09 16:22:45

``` python
from peewee import *
from peewee import OP
from peewee import Expression

db = MySQLDatabase(app.config['MYSQL_DATABASE_DB'], host=app.config['MYSQL_DATABASE_HOST'], user=app.config['MYSQL_DATABASE_USER'], passwd=app.config['MYSQL_DATABASE_PASSWORD'])

OP['MOD'] = 'mod'

def mod(lhs, rhs):
    return Expression(lhs, OP.MOD, rhs)

MySQLDatabase.register_ops({OP.MOD: '%'})

class Base(Model):
    class Meta:
        database = db

class User(Base):
    user_id = PrimaryKeyField()
    first_name = CharField(max_length = 150)
    last_name = CharField(max_length = 150)

@app.route('/')
def test():
    query = User.select().where(mod(User.user_id, 2) == 0)
    return "Query: %r" % query
```

---

If the call to `register_ops()` is moved before the DB is instantiated then everything works correctly.


Actually, I think this is acceptable behavior. It wouldn't be hard to use a second attribute to store instance-specific overrides and merge them when instantiating the `QueryCompiler`, but I think this is OK as-is.
Issue: Tests are not run for MySQL?
Author: pmeinhardt
State: closed
Created: 2015-05-08 13:52:44

Tests are only run for SQLite and PostgreSQL at the moment. Is there any particular reason the suite is not run against MySQL? After all `peewee` claims to support MySQL in the README and documentation.

See [.travis.yml](https://github.com/coleifer/peewee/blob/65bf15f93f2f96abf8b216e6eea056726711a19f/.travis.yml#L8-L10)

Cheers,
Paul


No they are not run for MySQL because there are some failures that I do not intend to fix. MySQL is supported, and most things work, but it is not tested as part of the continuous integration.
Issue: Feature Request: N+1 query detection
Author: josephschorr
State: closed
Created: 2015-05-07 20:21:00

It would be really nice if there was a clean way to detect N+1 queries in tests, perhaps by having peewee (when initialized with a flag) raise an exception when accessing a non-cached ForeignKeyField value.


That's a neat idea, but I'm not sure how to implement that without cluttering up the `ForeignKeyField` / `RelationDescriptor` classes.

One thing you can do currently, which is very easy, is to use the `assert_query_count` helper in `playhouse.test_utils` ([documentation link](http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#assert_query_count)):

``` python
class TestMyApp(unittest.TestCase):
    @assert_query_count(1)
    def test_get_popular_blogs(self):
        popular_blogs = Blog.get_popular()
        self.assertEqual(
            [blog.title for blog in popular_blogs],
            ["Peewee's Playhouse!", "All About Huey", "Mickey's Adventures"])

    def test_expensive_operation(self):
        with assert_query_count(1):
            perform_expensive_operation()
```

I think these are probably a better way of ensuring correct query behavior than checking for accidental foreign key resolutions anyways.
Issue:  __init__() got an unexpected keyword argument 'abstract' on peewee 2.04
Author: jbpineiroc
State: closed
Created: 2015-05-07 01:38:43

  File "C:\Python27\lib\site-packages\peewee.py", line 1801, in **new**
    cls._meta = ModelOptions(cls, **meta_options)
TypeError: Error when calling the metaclass bases
    __init__() got an unexpected keyword argument 'abstract'


Without a little more info I have no idea what to do with this.

Next time take the two extra seconds to format your issue at least.
Issue: Having a single multi-column index defined in Meta class breaks table/index creation
Author: josefdlange
State: closed
Created: 2015-05-06 23:14:08

Given the following, table/index creation fails:

```
from peewee import Model, ForeignKeyField
from somewhere import SomeOtherModel

class SomeModel(Model):
    something = ForeignKeyField(SomeOtherModel)
    name = CharField()

    class Meta:
        indexes = (
            (('something', 'name'), True)
        )
```

This is because Python (as far as I can tell) throws away the outermost parentheses as extraneous, so when you iterate over `indexes` on line `4011`, it actually is iterating on `(('something','name'), True)`, meaning that `fields` and `unique` in this case are `'something'` and `'name'`, respectively. If I change the code to the following, it works just fine:

```
    ...
    class Meta:
        indexes = tuple([
            (('something', 'name'), True)
        ])
```

If you like, I can try and figure out a way around this -- I think the parentheses-only code looks a lot better -- and submit a pull request. Or maybe we do want the outermost structure to be a list and not a tuple at all. Your call, just thought I'd report this.


This is not at all a peewee bug. You just forgot that if you want a tuple with one element you need a trailing comma:

``` python
    class Meta:
        indexes = (
            (('something', 'name'), True),  # Added trailing comma.
        )
```


D'oh! I learn something new every day. Thanks for the tip.
Issue: Query add_column method
Author: stanep
State: closed
Created: 2015-05-06 15:30:57

Hi , is it possible to add method to query class so we can do something like this

qr = User.select(User.id)

later on
qr.add_column(User.first_name)


No. You can muck around with `SelectQuery._select`, which is a list of the selected columns. Even better is to just use the `SelectQuery.select()` method to modify the list of columns.


Ok , fair enough

On Wed, May 6, 2015 at 11:46 AM, Charles Leifer notifications@github.com
wrote:

> Closed #593 https://github.com/coleifer/peewee/issues/593.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/coleifer/peewee/issues/593#event-298363133.
Issue: Hybrid module
Author: stanep
State: closed
Created: 2015-05-05 17:32:29

Hi when you do 
pip install -U peewee 
playhouse get installed but hybrid.py is missing


Yeah, I haven't issued a new release yet.


Anyway great thing hybrid properties,thx

On Tue, May 5, 2015 at 1:37 PM, Charles Leifer notifications@github.com
wrote:

> Closed #592 https://github.com/coleifer/peewee/issues/592.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/coleifer/peewee/issues/592#event-297451694.