akumoth/peewee-issues huggingface dataset:
Issue: [Question] Is there a way to get the display instead of the value for a field with a choices parameter associated with it?
Author: EPadronU
State: closed
Created: 2015-02-03 22:05:17

Hello, everyone.

In the following code:

```
class Model(db.Model):
    field = pw.Charfield(
        choices=[1, 'foo']
    )

    def __unidoce__(self):
        return self.<api call I don't know about>
```

I would like to print **foo** instead of **1**. The ORM implements a way to achieve this?

Thanks in advance.


It's just Python, there's no magic there. One easy way to accomplish this is to write something like this:

``` python

class Tweet(Model):
    STATUS_CHOICES = (
        (0, 'Draft'),
        (1, 'Published'),
        (9, 'Deleted'))
    status = IntegerField(choices=STATUS_CHOICES)

    def get_status_label(self):
        return dict(self.STATUS_CHOICES)[self.status]
```


Thank you very much.


Hello everyone,
and how to display labels in select query? (I need display labels later in jinja template)
```python
# some view
orders = Order.select(Order.id, Order.date,
                      Order.status.get_status_label()).order_by(Order.date)

# some jinja template
{% for order in orders %}
{{ order.id }} {{ order.status }}
{% endfor %}
```
Thanks

If you want it as part of the SQL you probably want a `CASE` statement mapping integer statuses to the labels. See the `Case()` helper in the docs: http://docs.peewee-orm.com/en/latest/peewee/api.html#Case

Alternatively, you could avoid "select"-ing a label and just calculate it in Python:

```python
# some view
# note we select the status column
orders = Order.select(Order.id, Order.date, Order.status).order_by(Order.date)

# some jinja template
{% for order in orders %}
  {# status column is populated with the status value, so we can use the helper method to get label #}
  {{ order.id }} {{ order.get_status_label() }}
{% endfor %}
```

Ok, your solution works. Before it I have tried:

```python
{% for order in orders %}
  {# status column is populated with the status value, so we can use the helper method to get label #}
  {{ order.id }} {{ order.status.get_status_label() }}
{% endfor %}
```
...but this not work. Your answer works as I expected.

Thank you very much and thanks for your work on peewee.
Issue: Not sure why: OperationalError: near "AS": syntax error
Author: kristofer
State: closed
Created: 2015-02-03 20:59:22

So I am getting a syntax error deep inside of peewee. 
I'm trying to get the "encrypted-flask-aws" app, pretty much unmodified,
when I try to do a GET on the root of the app,
I get:

```
...venv/lib/python2.7/site-packages/peewee.py", line 2855, in execute_sql
    cursor.execute(sql, params or ())
OperationalError: near "AS": syntax error
```

Just before this error, it run this... 

```
    return render_template('index.html', files=files, breads=breads, path=path)
```

and just before that when I "print files", I have

```
<class '__main__.File'> SELECT "t1"."id", "t1"."filename", "t1"."created_date", "t1"."encrypted", "t1"."folder", "t1"."parent" FROM "file" AS t1 WHERE ("t1"."parent" = ?) ORDER BY ("file" AS t1, ?, ?), ("file" AS t1, ?) [u'/', 'folder', 'DESC', 'filename']
```

And that SQL is being generated for me by Peewee, as it tries to query my sqlite.

any ideas??


It would be really helpful if you could paste the Python code that generated the `File` query.


The problem appears to be in how you are sorting your files. Maybe try modifying it to be:

``` python
files = File.select().where(File.parent == '/').order_by(File.folder.desc(), File.filename)
```


Yes, this was the line that created the files

```
    files = File.filter(parent=session['folder']).order_by((File,'folder','DESC'),(File,'filename'))
```


yep, I replace mine with yours and it works. Thanks for your help.


No problem! I think you were using the old pre-1.0 syntax. You might check http://peewee.readthedocs.org/en/2.0.2/peewee/upgrading.html#upgrading if you run into similar issues.


but then, real quick, how would I change

```
foo = File.filter(filename__icontains=someSubstring).order_by(File.folder.desc(), File.filename)
```

I keep getting a **AttributeError: type object 'File' has no attribute 'icontains'**
as you might guess it's a search term.


``` python
foo = File.select().where(
    File.filename.contains(someSubstring)).order_by(File.folder.desc(), File.filename)
```

This is all documented: http://docs.peewee-orm.com/en/latest/peewee/querying.html#query-operators


yep, that's handy. Thanks again.
Issue: JDBC support
Author: SamuelMarks
State: closed
Created: 2015-01-31 07:29:57

Has anyone tried using peewee with JDBC, e.g.: using [this wrapper](https://pypi.python.org/pypi/JayDeBeApi/) ([code](http://bazaar.launchpad.net/~baztian/jaydebeapi/trunk/view/head:/src/jaydebeapi/dbapi2.py)) which exposes DB-API v2.0 support?


This type of discussion might be more suited for the discussion group: https://groups.google.com/group/peewee-orm
Issue: Pooled Connections, MasterSlave and closing connections
Author: grvhi
State: closed
Created: 2015-01-29 18:18:25

I'm a little confused as to the process of opening and closing connections when using PooledPostgresqlExtDatabase and ReadSlaveModel... Say I have one master and one read. When opening a connection in my app (celery task), do I need to explicitly open a connection to both databases? Or do I open a connection to the master database only? 

In the following example, how should I manage connections?

``` python
master = PooledPostgresqlExtDatabase()
read = PooledPostgresqlExtDatabase()

### Models
class SomeModel(ReadSlaveModel):
    something = TextField()

    class Meta(object):
        """
        Set the required Meta values
        """
        database = master
        read_slaves = (read, )

##### App
try:
    SomeModel.get(SomeModel.id == 1)
except:
    SomeModel.create(**attributes)
```


For a celery task you will probably want to open/close the connections to both databases, if you are going to be using both in the task, e.g.

``` python
master.connect()
read.connect()
try:
    my_code()
finally:
    master.close()
    read.close()
```

The pool will handle recycling the connections for you.


Great, thanks @coleifer
Issue: "where"-chaining does not work with negated BooleanFields
Author: tfeldmann
State: closed
Created: 2015-01-29 09:36:31

Hello,

when chaining ".where()" expressions and using negations the query works only if the negated expression comes last. See the attached example code:

``` python
from peewee import *

db = SqliteDatabase(':memory:')

class Test(Model):

    flag = BooleanField()
    num = IntegerField()

    def __repr__(self):
        return '<Test(flag=%s, num=%s)>' % (self.flag, self.num)

    class Meta:
        database = db


db.create_tables([Test])
Test.create(flag=False, num=10)
Test.create(flag=True, num=-1)
Test.create(flag=False, num=-2)
Test.create(flag=False, num=12)

# this works:
print(list(Test.select().where((~Test.flag) & (Test.num > 0))))
print(list(Test.select().where((Test.num > 0) & (~Test.flag))))
print(list(Test.select().where(Test.num > 0).where(~Test.flag)))
print(list(Test.select().where(Test.flag == False).where(Test.num > 0)))

# this throws "AttributeError: 'NoneType' object has no attribute '_negated'"
print(list(Test.select().where(~Test.flag).where(Test.num > 0)))
```

Tested with Python 3.4.2 and peewee 2.4.6 on Mac OS X 10.10.2
Issue: Subqueries in simple `delete_instance()` calls
Author: coleifer
State: closed
Created: 2015-01-28 00:12:24

> We've been using peewee for quite a while now and have noticed from time-to-time that peewee generates delete (and update for nullables) queries of the form:
> 
> ``` sql
> UPDATE `table` SET `field` = null 
> WHERE (`table`.`field` IN (
>     SELECT `t2`.`id` FROM `othertable` AS t2 WHERE (`t2`.`id` = 1234)))
> ```
> 
> Earlier today I was debugging a slowdown on a table of approx 11 million rows in MySQL, when I noticed that the above SQL, if run without the subquery, drops from approx 20s to a few ms. Is there a reason that peewee generates foreign key updates/deletions with this form vs a direct 'table.field = 1234'? I was thinking of submitting a PR to change peewee to use the more direct SQL form, but I want to make sure I fully understand the problem space before addressing it this way.

My response:

> The reason they're generated this way is because when you call `delete_instance()` Peewee generates a dependency graph. As the graph is traversed, peewee progressively builds more complex subqueries. This works, but I can see how in the simple case we could get away with a simple equality check.


Fixed by 08c595a


Hi @coleifer,

Has this issue regressed at some point in version 3.x.x? We're looking to upgrade to the latest version of peewee and we're once again seeing deletion queries of this form when calling recursive deletes:

```
DELETE FROM "table2" WHERE ("foreign_field" IN (SELECT "t1"."id" FROM "table1" AS "t1" WHERE ("t1"."id" = ?)))
```


Fixed (again)!
Issue: UUID foreign keys are not cast properly
Author: alexlatchford
State: closed
Created: 2015-01-27 22:16:08

Found this out by trying to use prefetch and it failing for no reason. I thought it was a problem with the id_map not find the appropriate relation to match on because the data is coming back but simply it's a type mismatch, one is a string and the other is a UUID thus no match :(

https://github.com/coleifer/peewee/blob/2.4.6/peewee.py#L3963

Example:

``` python
class Parent(Model):
    uuid = UUIDField(primary_key=True)

class Child(Model):
    name = CharField()
    parent = ForeignKeyField(Parent)

parents = Parent.select()
children = Child.select()

parents_prefetch = prefetch(parents, children)
```

This should show you the problem. Take a close look at the keys in the id_map, they're the problem :)

I've also tried to get `.aggregate_rows()` to work to no avail too, guessing it's the same root problem.

Thanks again for the great library Charles!


Awesome thanks charles!
Issue: query results length
Author: dpep
State: closed
Created: 2015-01-26 17:29:22

a couple times i tried to call len() on the results of a query and it exploded...figured this would be good functionality to add.  if you only want a count, i realize that a SELECT COUNT(*) should be used, but there are also times when it would be nice to see how many results i got and use them


Can you define "exploded"?


There is already the `SelectQuery.count()` method, which handles some edge-cases, so I'm loth to add another method that does the same thing.


exploded = throws an exception

> TypeError: object of type 'SelectQuery' has no len()

it just felt like the query results object is an iterable container class that should implement the len() function so you can figure out how many results were returned prior to iterating through them.  otherwise, you have to do something like

```
for res in results:
    do_stuff_with_res(res)
    count += 1
print "you have %d results" % count
```

using Select.count() requires that you either only want a count and not the resulting rows, or else that you have to use two queries.  one snag i found:

> Note that it is not uncommon for databases to report -1 after a select statement for performance reasons. (The exact number may not be known before the first records are returned to the application.) - https://code.google.com/p/pyodbc/wiki/Cursor

this might explain all the Travis failures


There are quite a few test failures resulting from peewee checking the truthiness of the value.


If you want the length of the result set, you can always do `len(list(query))`.

I think I'm going to pass on merging this and mark wontfix.


Why not adding this to SelectQuery:

```
    def __len__(self):
        return self.count()
```

Calling `len` is very pythonic, and is missing imho.

> If you want the length of the result set, you can always do len(list(query)).

Is this issuing a count on db level, or python level?


As I mentioned above, there are places that check the truthiness of a query variable. Adding a `__len__` method would cause the interpreter to use the query's count for the truth check.

This may be worth looking into, though.


I looked again and whatever tests had been failing appear to have been fixed. I've added your changes.
Issue: default values
Author: dpep
State: closed
Created: 2015-01-22 23:53:34

field default values were being ignored for column creations and modifications.  this change ensures that a field marked with a default value creates a default value in the db.  tested this change through the standard Model table creation and schema migrator add_column() function, in mysql and sqlite


Peewee makes a point of not setting default DB values, so the current behavior is correct.


was it too complicated to get right across platforms?


My feeling was that it could lead to confusing behavior on the python side. If you really want it, you can always use SQL to accomplish default DB values, but it wasn't something I wanted to add to peewee.
Issue: Duplicate field names after stripping '_id' suffix
Author: teeberg
State: closed
Created: 2015-01-22 20:09:41

For reasons that are beyond me, I'm working on a database that often has fields which have the same name except for the `_id` part at the end, that is one called e.g. `language` and one `language_id`. Generating models for this table using pwiz gives me model definitions like:

```
language = CharField()
language = IntegerField(db_column='language_id')
```

Other than that, amazing how little tweaking I had to do! Thank you for the awesome ORM!


That was an awesome response time! :-) Thanks so much!


NP! I really appreciate the bug report.
Issue: Revert "Fix Model.create_table(fail_silently=True) if schema is used."
Author: stas
State: closed
Created: 2015-01-22 11:27:23

This commit introduces a regression by overwriting the `PostgresqlDatabase.get_tables()` default value for `schema`.

``` python
(Pdb) db
<playhouse.pool.PooledPostgresqlExtDatabase object at 0xb620964c>
(Pdb) db.get_tables(schema=None)
[]
(Pdb) db.get_tables()
[u'migration', u'users']
```

One of the affected projects is https://github.com/cam-stitt/arnold
Basically migrations no longer go down.

@dkorduban sorry to ask for a revert, please provide and update the tests if you really want to fix this issue.

@coleifer please try to encourage the presence of tests in contributions :cry: 


I took a different approach that should work the same.


Thanks for taking care of it!
Issue: key renames
Author: dpep
State: closed
Created: 2015-01-22 09:35:20

MySQL does wierd things when altering the columns of unique or primary keys.  It acts as though the user is tring to duplicate the field, rather than simply changing / renaming it (see error messages below).  Dropping these keywords from the query gives us the descired outcome anyway (keys remain keys without need to reiterate their definition).  wasn't sure whether it was better to leave the code in and comment it out with an explanation or go the cleaner route...

Warning: Duplicate index 'old_id' defined on the table 'test.data'. This is deprecated and will be disallowed in a future release.

peewee.OperationalError: (1068, 'Multiple primary key defined')
Issue: Not support self aggregate alias join any more?
Author: BusyJay
State: closed
Created: 2015-01-22 06:43:27

Hi, before release 2.4.6, I was able to find out the most recent 5 articles and its comments via:

```
article = Article.select(Article).order_by(
                Article.publish_time.desc()).paginate(1, 5).alias("ar")
for a in Article.select(Article, Comment).join(Comment).join(
                article, on=(Article.id==article.c.id)).aggregate_rows():
    print a
```

But after upgrade to 2.4.6，it raise exception as follow:

```
/home/me/devenv/pyvirt/lib/python2.7/site-packages/peewee.pyc in next(self)
   1774             return inst
   1775 
-> 1776         obj = self.iterate()
   1777         self._result_cache.append(obj)
   1778         self.__ct += 1

/home/me/devenv/pyvirt/lib/python2.7/site-packages/peewee.pyc in iterate(self)
   2040                 else:
   2041                     backref = current._meta.reverse_rel_for_model(
-> 2042                         join.dest, join.on)
   2043                     if not backref:
   2044                         continue

/home/me/devenv/pyvirt/lib/python2.7/site-packages/peewee.pyc in reverse_rel_for_model(self, model, field_obj)
   3562 
   3563     def reverse_rel_for_model(self, model, field_obj=None):
-> 3564         return model._meta.rel_for_model(self.model_class, field_obj)
   3565 
   3566     def rel_exists(self, model):

AttributeError: 'SelectQuery' object has no attribute '_meta'
```

A quick test snippets:

```
import peewee
import datetime

db = peewee.SqliteDatabase(":memory:")

class BaseModel(peewee.Model):
    class Meta:
        database = db


class Article(peewee.Model):
    id = peewee.PrimaryKeyField()
    publish_time = peewee.DateTimeField(default=datetime.datetime.now)


class Comment(peewee.Model):
    id = peewee.PrimaryKeyField()
    owner_id = peewee.ForeignKeyField(Article)

db.connect()
db.create_tables([Article, Comment], True)

for _ in xrange(21):
    t1 = Article.create()
    Comment.create(owner_id=t1)
    Comment.create(owner_id=t1)

article = Article.select(Article).order_by(
                Article.publish_time.desc()).paginate(1, 5).alias("ar")
for a in Article.select(Article, Comment).join(Comment).join(
                article, on=(Article.id==article.c.id)).aggregate_rows():
    print a
```


Ouch, thanks! Will look into this after I get off work today.


Should be fixed in f4481d14d8fc08b5c4c9964d7838af2866f021fd


I'll re-push 2.4.6


verified, thx!
Issue: Constraints not created by db.create_table(Class1), yes by db.create_tables([Class1])
Author: chrisinmtown
State: closed
Created: 2015-01-20 14:31:42

In peewee 2.4.5 my tests show that methods create_table() and create_tables() differ in creation of uniqueness constraints. If I use db.create_table(Class1) then my request for uniqueness constraints is silently ignored. If I use db.create_tables([Class1]) then the constraints are created as expected.  

I'm using MySQL 5.5, connector is PyMySQL 0.6.3.  The model below is directly from the documentation; mine is very similar. 

```
class Transaction(Model):
    from_acct = CharField()
    to_acct = CharField()
    amount = DecimalField()
    date = DateTimeField()

    class Meta:
        indexes = (
            # create a unique on from/to/date
            (('from_acct', 'to_acct', 'date'), True),

            # create a non-unique on from/to
            (('from_acct', 'to_acct'), False),
        )
```

I expect a clause in the table definition something like this (sorry this is typed not pasted):

```
UNIQUE KEY `from_acct_to_acct_date` (`from_acct`,`to_acct`,`date`)
```

I was put on the trail by issue https://github.com/coleifer/peewee/issues/211.  This is quite a subtle difference!  Please harmonize. Thanks for listening and for a very, very useful package.


The `db.create_table()` method will only create the table, not any of the indexes or constraints. To create a table for a model along with constraints, use `ModelClass.create_table()` or if creating multiple tables, `db.create_tables()`. I understand this is confusing and will update the docs to clarify the difference.


I thank you for updating the documentation. Forgive my grumbling, but my programmer's heart is saddened by enshrining in code & doc different behaviors for functions that at first glance seem to be nearly identical, just one works on a single argument and the other on a list of arguments. 


There are historical reasons for this, but I do agree that it can be confusing. The documentation examples all should be showing either `Model.create_table()` or `Database.create_tables()`.
Issue: ManyToManyField in Shortcuts
Author: karelv
State: closed
Created: 2015-01-16 13:30:21

ManyToManyField in Shortcuts module is not working in 2.4.5

``` py
from playhouse.shortcuts import ManyToManyField
```

yields in:

```
from playhouse.shortcuts import ManyToManyField
ImportError: cannot import name 'ManyToManyField'
```

So it finds the module, but not the class ManyToManyField...

Note: I refer to the example at:
http://peewee.readthedocs.org/en/latest/peewee/querying.html#manytomanyfield
Where the import statement is used.


Yeah, it's currently only in master. I will be pushing a new version soon which will contain the field.
Issue: Why is so much of the logic in a single file?
Author: ianks
State: closed
Created: 2015-01-16 05:51:33

Is there a reason that peewee.py contains so much of the logic? Surely this can be broken up into different files. Is there an advantage to doing it this way?


A single file, that's all you need!

If it were to get to 10K lines I would consider splitting it up, but I don't imagine it will get that big. The code is also already broken up into the core and the `playhouse` extensions collection.

Look around at sqlalchemy, you'll see a number of 3K line files. Django has several 1K+ line files.


My experience of new comer is that this is not so welcoming:
- I'm missing an overview of the API structure: who inherits who? which logical groups do we have (Model, Fields, Query, Database…)? Where should I go to understand this piece of logic?
- each time a search for something in the code, I'm missing the context (which class am I? Where in the class hierarchy?)
- my _own_ perception is that files bigger than around 1k lines are bad practice (mainly for readability, i.e. reasons exposed before), so seeing this huge file made me a bit reluctant being confident in peewee as a serious project when I first considered it for a new project I'm starting

Also, I'd suggest to namespace `playhouse` (and call it something more expected like "extras" or "extensions"). I've been reluctant using playhouse module at the first time, because the name suggested me it was more about `playground` or `examples` actual extensions. Also, I was more or less feeling peewee was more of a POC/pet project because of that.

My two cents… ;)


Peewee has always been a single module. It will remain that way. Honestly there are very few concepts in peewee, and if you read through the entire source, you will have a good grasp on it. Projects that are big, like some other orms, you can't reasonably read. And there's no cohesive order, either. Peewee reads like a book, in order.

Good point about playhouse, need to make it clear those are extensions.


Not to start again the discussion, but for the record: one other drawback about the single file package I've run into: git(hub) blame and history are very noisy because all changes are in the same file, and thus imho hardly usable.
Issue: error with DateTimeField
Author: daufinsyd
State: closed
Created: 2015-01-15 21:42:28

Hello,
I can't filter objects from with DateTimeField (with Sqlite), it always return

> File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 2825, in execute_sql
>     cursor.execute(sql, params or ())
> peewee.OperationalError: user-defined function raised exception

I have something like this:

> class A(Model):
>     dateA = DateTimeField()
> def Function(self):
>     for a in (A.select().where(A.dateA.year == 2015)):
>        print('smthg')

but if I use

>    for a in (A.select().where(A.dateA == datetime.datime.now()):

It works

and

> print(A.select().where(A.dateA.year == 2015).count())
> raise the same error.

i use sqlite and connect the db with:

> db = SqliteDatabase('db.sqlite')
> def connectDb():
>     db.connect()
>     db.create_tables([A], true)

type:

> a = A.get(id=1)
> type(a.dateA)
> <class str>

I didn't found any solution over Internet, do you have any idea ?
Thank you


What do the dates in your table look like? Can you show me some examples?


it's
15/01/2015 22:15


OK, that's the problem. You'll need to subclass `DateTimeField` and specify the `formats` parameter, e.g.

``` python
class CustomDateTimeField(DateTimeField):
    formats = ['%d/%m/%Y %H:%M'] + DateTimeField.formats
```