akumoth/peewee-issues huggingface dataset:
Issue: IN operator and subqueries
Author: arielrossanigo
State: closed
Created: 2012-12-19 21:18:39

This patch allows you to make a query with an IN operator where the subquery return another field than the "id".

For example, I have this two models:

``` python
    class Customer(Model):
        cust_number = IntegerField()
        name = CharField()
     ....

    class AccountLog(Model):
        name = CharField()
        internal_number = IntegerField() # this number is filled with the cust_number
    .....
```

and I want to obtain all the Customers who doesn't have an account created. 
This query resolve the problem

``` python
    created_accounts = AccountLog.select(AccountLog.internal_number)
    q = Customer.select().where(~(Customer.cust_number << created_accounts))
```

With this patch, the resulting SQL is:

``` SQL
SELECT t1."id", t1."cust_number", t1."name"  
FROM "customer" AS t1 
WHERE NOT (t1."cust_number" IN (
        SELECT t2."internal_number" 
        FROM "accountlog" AS t2))
```

Sorry for my English.... 


Cool, i think we definitely need this.  I think the extra state might be avoidable if we bind the selection a little later.  I will look into that approach.
Issue: ImportError: cannot import name BinaryExpr
Author: chrispaolini
State: closed
Created: 2012-12-19 18:31:07

It seems that there is a call to "BinaryExpr", but that doesn't exist.
Looks like it was removed in: 8f8e195480932a274cb3f9921538dfacb47d0bc7


Yeah, binaryexpr is replaced with "Expr" - I did a refactor recently of those pieces in the commit you referenced.
Issue: R() objects and cloning
Author: coleifer
State: closed
Created: 2012-12-19 14:44:30

<MikeSeth> [07:57:26] how do I do INTERVAL?
<MikeSeth> [07:57:56] e.g. I have an expression like NOW() - INTERVAL 1 HOUR
<MikeSeth> [08:15:55] nevermind, R()
<MikeSeth> [08:17:47] AttributeError: 'R' object has no attribute 'clone'
<MikeSeth> [08:17:49] hm.
<MikeSeth> [08:20:17] R isn't Expr?
<MikeSeth> [08:30:02] nevermind, RawQuery () :P


Based on the testcases I committed, I can't replicate this error: 624a0e19ab085f3cd0d7
Issue: Select a random record from db peewee 2.04
Author: hernantz
State: closed
Created: 2012-12-05 02:06:58

Any example of how to get a random object from a model?
Thanks in advance.


You probably want something like this:

``` python

from peewee import *

random_query = SomeModel.select().order_by(fn.Random())
one_obj = random_query.get()
```


Thanks! worked.
Is this in the docs?


Nah, i'll add it maybe but its standard 'sql' practice.
Issue: Ordering in 2.0.4
Author: dkadish
State: closed
Created: 2012-12-04 01:03:06

In the Meta class in v1, ordering is done by

``` python
class Meta:
    ordering = (('col1', 'desc'), ('col2', 'asc')
```

In version 2, this has changed to:

``` python
class Meta:
    order_by = ('-col1', 'col2') # I think I have my signs right here...
```

That's not reflected in the docs in https://peewee.readthedocs.org/en/latest/peewee/upgrading.html.
https://peewee.readthedocs.org/en/latest/peewee/models.html hints at it, but the example still shows the old `ordering` instead of `order_by` which throws an error.
Issue: Integer Primary Key won't save the data
Author: coderbuzz
State: closed
Created: 2012-11-24 13:00:49

``` python
class TableMargin(BaseModel):

    class Meta:
        db_table = 'tabel_margin'

    periode = IntegerField(primary_key=True)
    margin = FloatField()

def set_margin(periode, margin):
        query = TabelMargin.select().where(TabelMargin.periode==periode)

        with db_transaction():
            item = first_record(query) or TabelMargin()
            item.periode = periode
            item.margin = margin
            item.save()
            return item

margin = 0.5000
for n in xrange(1, 61):
    margin += 0.0016
    set_margin(n, margin)
```

Code above won't  save the data to database.
Changed to the following structure the results works-well; but I DON'T need the ID field because PERIODE field already unique (it should be a PrimaryKey)

``` python
class TableMargin(BaseModel):

    class Meta:
        db_table = 'tabel_margin'

    periode = IntegerField(unique=True)
    margin = FloatField()
```


So...you want periode to be the primary key _and_ you want it to be the values from 1 -> 60.  Explicitly setting the primary key can be accomplished in a couple of ways...  I'd suggest reading the code for the save() method.

You can `save(force_insert=True)`.  You can manually create and execute an InsertQuery (recommended way).

iq = TableMargin.insert(periode=x, margin=y)
iq.execute()


OK, so I changed the code and now works as expected (auto insert/update):

``` python
def set_margin(periode, margin):
        query = TabelMargin.select().where(TabelMargin.periode==periode)

        with db_transaction():
            item = first_record(query) or TabelMargin()
            force_insert = item.periode == None
            item.periode = periode
            item.margin = margin
            item.save(force_insert=force_insert)
            return item
```
Issue: unique index creation breaks on mysql innodb + utf8mb4 + charfield
Author: jamesonjlee
State: closed
Created: 2012-11-22 00:06:12

hi,

I found that the default CharField() is a varchar of length 255, this makes mysql throw error 1071 because in innodb the maximum length of a index is 767 bytes (~190 utf8mb4).  default utf8 will fit 255 chars.

Would it be possible to have peewee detect that the database is using utf8mb4 and adjust it accordingly? or should I be creating a UTF8MB4CharField() and make it do CharFIeld(max_length=190) ?


Dude...

Just subclass charfield and provide a default maxlength and use that.
Issue: TypeError: 'NoneType' object does not support item assignment when trying to select from tables after 0.9.2 to 2.0.4 upgrade
Author: skruger
State: closed
Created: 2012-11-20 02:46:27

I'm upgrading my application to the current version of peewee and many of my models are failing the same way when I try to query them.  Most of my accesses happen within @classmethod calls so I did one manually from the command line and I get the same error.  I'm not sure what I'm doing wrong or what other details might help with this, but I thought I'd see if this was a known problem and I'm just messing up something simple.  Nothing has changed about the definition of this table in my upgrade process.  However, I did define a new model, called create_table(), and it seems to work.

Here is the stack trace.  I'm running python2.7 and installed peewee 2.0.4 using pip.

``` python
>>> XmppDomain.select().where(XmppDomain.fqdn == "kxgx.chatwith.it").get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 1285, in get
    return clone.execute().next()
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 979, in next
    instance = self.iterate()
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 965, in iterate
    return self.simple_iter(row)
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 899, in simple_iter
    setattr(instance, f.name, f.python_value(row[i]))
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 218, in __set__
    instance._data[self.att_name] = value
TypeError: 'NoneType' object does not support item assignment
```


Are you overriding **init** on the model class?  Also, Xmpp!  Cool!


I wasn't implementing init in my models directly so I didn't think the solution from issue #118 was applicable until I checked my BaseSql class.  I added the fix from #118 to BaseSql and that resolved my problem.

``` python
class BaseSql(peewee.Model):

    class Meta:
        database = database
    def __init__(self, *args, **kwargs):
        super(BaseSql, self).__init__()
        ... etc ...

class XmppDomain(BaseSql):
    .... etc ....
```

It sounds like you like XMPP.  The app I'm working on is the chatmongers.com management portal.  We picked peewee when we decided to port the app out of Google App Engine.


Awesome glad that fixed it!  Very cool site, i'm flattered you picked peewee!


just ran in to a similar issue, fixed by calling super at the top of my model's **init** as referenced in #118
Issue: "Offline" support for ForeignKeyField (related_name)
Author: kr2
State: closed
Created: 2012-11-18 15:30:25

I think it would be nice if one could do something like below. So stepping though the "children" before saving them.

``` python
import peewee as pw

class Parent(pw.Model):
    name = pw.TextField()

class Child(pw.Model):
    parent = pw.ForeignKeyField(Parent, related_name='children')
    name = pw.TextField()

p1 = Parent(name = 'p1')
c1 = Child(parent = p1, name = 'c1')
c2 = Child(parent = p1, name = 'c2')

for child in p1.children:
    print child.name
```


The implementation of reverse relations is a SelectQuery, which requires a database call.
Issue: Create Table Failed
Author: coderbuzz
State: closed
Created: 2012-11-18 04:39:58

``` python
class SomeModel(BaseModel):

    nama = CharField(max_length=30, index=True)
    kategori = CharField(max_length=30, index=True)
    value = CharField(max_length=255, null=True)
    keterangan = CharField(max_length=255, null=True)


SomeModel.create_table()
```

_Traceback (most recent call last):
  File "D:\Codes\Python\Kisel-SIMPIN\main.py", line 3, in <module> from app import app
  File "D:\Codes\Python\Kisel-SIMPIN\app.py", line 54, in <module> import imports
  File "D:\Codes\Python\Kisel-SIMPIN\imports.py", line 6, in <module> import model
  File "D:\Codes\Python\Kisel-SIMPIN\model.py", line 478, in <module> SomeModel.create_table()
  File "build\bdist.win32\egg\peewee.py", line 1968, in create_table ValueError: too many values to unpack_


I wasn't able to replicate this locally.  What commit/version of peewee do you use and what database backend are you using?  Was there any more information in that traceback?

Part of why I'm confused: https://github.com/coleifer/peewee/blob/master/peewee.py#L1968  --- blank?


Wow I'm using Unstable\2.0 version,

``` python
 1967        if cls._meta.indexes:
 1968           for fields, unique in cls._meta.indexes:
 1969                db.create_index(cls, fields, unique)
```

OK trying pull the master branch, it is 2.0 version rights?


Pulled from master branch, no problem here. Sorry for this, case closed.
Issue: update query overwrites id with single value
Author: davidthewatson
State: closed
Created: 2012-11-09 05:15:55

I have the following models, mostly generated from pwiz. I'm using peewee 2.0.3 on linux with mysql.

```
class Users(BaseModel):
    """The users database model."""
    fbid = BigIntegerField()
    fbname = CharField()
    lastsession = CharField()
    playedts = IntegerField()

    class Meta:
        db_table = 'users'

class Ornaments(BaseModel):
     """The ornaments database model."""
     dedicatedto = CharField()
     likes = IntegerField()
     message = CharField()
     userid = ForeignKeyField(Users, db_column='fbid')
     x = IntegerField()
     y = IntegerField()
```

I have the following code:

```
    print ids, likes
    assert(len(likes) == len(ids))
    with database.transaction():
        for id, like in zip(ids, likes):
            print id, like
            o = Ornaments.update(id=id, likes=like)
            o.execute()
    print len(likes), 'records updated at', datetime.now()
```

which outputs:

```
[1, 2, 3] [0, 0, 0]
1 0
2 0
3 0
3 records updated.
[3, 3, 3] [0]
```

and then fails on the following assert because the ids and likes lists aren't parallel. I don't understand why the id is getting updated with the constant 3 instead of the 1,2,3 sequence indicated by the print output?

Thanks!


Updates follow the sql update query syntax, so in your example the queries executed are:

``` sql
update ornaments set id=%s, likes=%s
```

What you want is:

``` sql
update ornametns set likes=%s where id = %s
```

So:

``` python
for id, like in zip(ids, likes):
    o = Ornaments.update(likes=like).where(Ornaments.id == id)
    o.execute()
```

Please let me know if this fixes the issue!


Yep, thanks.
Issue: Select with outer changes
Author: efreeze
State: closed
Created: 2012-10-31 08:58:15

Model:

``` python

database = MySQLDatabase('blah', **{...})
class BaseModel(Model):
    class Meta:
        database = database
class Zone(BaseModel):
    field = IntegerField()

Query on empty table:
Zone.select().where(Zone.field == 1).count()
```

It returns 0 items.

The application is running.
I'm insert into table new record (using phpMyAdmin):
INSERT INTO `zone` (`field`) VALUES (1)

Carry out a query for a non-empty table:
Zone.select().where(Zone.field == 1).count()
It returns 0 items too.
It's wrong, in table 1 record exists.
The application must be restarted.

If I make Database implement MySQLDatabase with property commit_select = True:
class Database(MySQLDatabase):
    commit_select = True
database = Database('blah', **{...})
Query returns 1 item. It's right.

Maybe you should make commit_select property changeable?

P.S. Sorry, I speak English bad :-)


Thanks for reporting.  I believe the problem is your connection isolation level is "read committed", meaning once your transaction begins (implicitly with the first select), it will only read rows committed before that transaction was initiated.  When you `commit()` on the connection and a new transaction begins, it picks up the new rows.

I think the solution here will be, like you say, to simply commit after select() queries.
Issue: AttributeError: 'MySQLDatabase' object has no attribute 'quote'
Author: kr2
State: closed
Created: 2012-10-29 17:41:16

C:\Python27\lib\site-packages\peewee-2.0.2-py2.7.egg\peewee.py in create_table(cls, fail_silently)
   2003         for field_name, field_obj in cls._meta.fields.items():
   2004             if isinstance(field_obj, ForeignKeyField):
-> 2005                 db.create_foreign_key(cls, field_obj)
   2006             elif field_obj.index or field_obj.unique:
   2007                 db.create_index(cls, [field_obj], field_obj.unique)

C:\Python27\lib\site-packages\peewee-2.0.2-py2.7.egg\peewee.py in create_foreign_key(self, model_class, field)
   1744 
   1745         query = framing % {
-> 1746             'table': self.quote(db_table),
   1747             'constraint': self.quote(constraint),
   1748             'field': self.quote(field.db_column),

AttributeError: 'MySQLDatabase' object has no attribute 'quote'
###### 

diry hack (copy and paste from the QueryCompiler class):
--- peewee.py   Mon Oct 29 17:34:38 2012
+++ peewee_OK.py    Mon Oct 29 17:32:29 2012
@@ -1730,6 +1730,9 @@ class MySQLDatabase(Database):
         conn_kwargs.update(kwargs)
         return mysql.connect(db=database, **conn_kwargs)
-    def quote(self, s):
-        return ''.join((self.quote_char, s, self.quote_char))
  +
   def create_foreign_key(self, model_class, field):
       framing = """
           ALTER TABLE %(table)s ADD CONSTRAINT %(constraint)s


This has actually been fixed for a while, I just need to push a new release.  Will do so now.
Issue: Create Unique index fails
Author: skid
State: closed
Created: 2012-10-29 09:08:06

I'm using Peewee 2.0.2 (installed with pip), my model looks like this:

```
class Settings(Model):
    key = CharField(max_length=25, index=True, unique=True)
    value = CharField()
    vtype = CharField(max_length=6)

    class Meta:
        db_table = 'settings'
```

When I run `Settings.create_table()` the following error happens:

```
File "......./peewee.py", line 2008, in create_table
    for fields, unique in cls._meta.indexes:
ValueError: too many values to unpack
```

When I check Settings._meta.indexes, it has the following value: `['key']` - hence the error.

If I comment these lines out - everything works fine since the field indexes are added immediately before this code is executed:

```
if cls._meta.indexes:
    for fields, unique in cls._meta.indexes:
        db.create_index(cls, fields, unique)
```

I know that these code should add the indexes defined in the `Meta` class - but in that case the indexes defined in the model field definitions should not be appended to self._meta.indexes.


Oh, I see this is fixed in the Github version... Sorry.


I'll push an update today or tomorrow
Issue: Support Microsoft SQL Servrr?
Author: YCF
State: closed
Created: 2012-10-19 02:53:50


Not as a part of peewee, but if you're interested in writing support it should not be too hard.

Might check out:
http://code.google.com/p/pyodbc/


http://peewee.readthedocs.org/en/latest/peewee/database.html
Issue: Any plans for supporting generic foreign keys?
Author: sigurdga
State: closed
Created: 2012-10-17 14:26:51

After spending a day to see if peewee is suitable for my project(s), I cannot see any way to use generic foreign keys, or any way to do a query that will join on explicit column names and values.

Tags would be a natural case for this, when you want to refer to "content type" and "object id" to be able to use tags all over without creating a lot of tables.

Django describes it as content types: https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/

The way I tried to do it was to have a CharField and an IntegerField, where the CharField contains the table name and the IntegerField the id. But I cannot figure out how to do the queries, as I am not able to join two tables without a foreign key.


If you want generic foreign keys, I would consider adding them as a part of the "playhouse" package.  You're right, they hinge on having some sort of additional framework that maps database entries to model classes (or other tables, if you prefer).  I'll take a look at this, as I have (ab)used django's GFKs a bit.


What do you think about an API like this:

``` python
class Tag(Model):
    tag = CharField()
    object_type = CharField(null=True)
    object_id = IntegerField(null=True)
    object = GFKField('object_type', 'object_id')

class Blog(Model):
    tags = ReverseGFK(Tag, 'object_type', 'object_id')

tag.object -> should be a blog
blog.tags -> select query of tags for ``blog`` instance
Blog.tags -> select query of all tags for Blog instances
```

Code: https://github.com/coleifer/peewee/blob/feature/gfk/playhouse/gfk.py

Branch: https://github.com/coleifer/peewee/tree/feature/gfk


Wow! I'm eager to try this out tomorrow. It looks like this approach should work, and I hope it will when testing it. I'll let you know how it works for me.

Thanks a million!


merged into master


Great! I have just checked it out and it works perfect with the rest of my code. Peewee query syntax is very self explaining and easy to remember. And it is a lot easier to understand your little example code above than the corresponding Django documentation.


I've made a simple generic foreign key implementation: https://gist.github.com/wonderbeyond/0e1f402b7595e0b4f9f653260fed029c#file-index-md

Thanks for sharing @wonderbeyond -- this module was removed in 3.0.

Why was this removed? Is there some other "official" way to handle the use case that @wonderbeyond's code solves?

>Why was this removed?

In my opinion, it's a gross hack and really does not belong in production-worthy code. It cannot be optimized in a generic way. It's only helpful for the simplest use-cases, but is very easy to have a footgun moment. A well-designed schema will obviate the need for this.

Implementation-wise, it requires a centralized registry of sorts to indicate the "content-type" that an arbitrary identifier references.

Also muddies the water in terms of introspection.

Thanks for the reply!
Issue: AttributeError: 'ForeignKeyField' object has no attribute 'to'
Author: kolanos
State: closed
Created: 2012-10-17 00:20:48

The create_foreign_key() function appears to be referencing an attribute that doesn't exist:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "env/local/lib/python2.7/site-packages/peewee.py", line 2003, in create_table
    db.create_foreign_key(cls, field_obj)
  File "env/local/lib/python2.7/site-packages/peewee.py", line 1739, in create_foreign_key
    field.to._meta.db_table,
AttributeError: 'ForeignKeyField' object has no attribute 'to'
```

This occurs while running create_table() on a model that has a ForeignKeyField().

Running peewee version 2.0.2.


Thanks for fixing this one so quickly! When is the next release planned? Right now I'm using the master branch to get around this bug.
Issue: Indexing and ordering troubles
Author: rcarmo
State: closed
Created: 2012-10-16 13:21:47

While trying to create an index on a table, I've come across an error that I can't fathom, but which appears to be due to some mismatch between what I can see on ReadTheDocs and currently working code.

Here's my sample model:

``` python
#!/usr/bin/env python

"""
Test Case
"""

import os, sys, logging, datetime, platform

logging.basicConfig(format='localhost - - [%(asctime)s] %(message)s', level=logging.DEBUG)
log = logging.getLogger(__name__)

from peewee import *

log = logging.getLogger()

db = SqliteDatabase('app.db')
db.connect()

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

class Queue(CustomModel):
    """Represents queued instructions for a client device"""
    scheduled   = DateTimeField(primary_key=True,unique=True,default=datetime.datetime.now)
    mac_address = CharField(null=False)
    action      = CharField(null=False)
    data        = CharField(null=True)
    sent        = BooleanField(default=False)

    class Meta:
        indexes = (
            (('mac_address'), False),
        )
        order_by = ('-scheduled',)


Queue.create_table()
```

When I run the script above, I get:

```
localhost - - [2012-10-16 14:16:10,647] ('CREATE TABLE "queue" ("scheduled" DATETIME NOT NULL PRIMARY KEY, "mac_address" VARCHAR(255) NOT NULL, "action" VARCHAR(255) NOT NULL, "data" VARCHAR(255), "sent" SMALLINT NOT NULL)', None)
localhost - - [2012-10-16 14:16:10,680] ('CREATE UNIQUE INDEX "queue_scheduled" ON "queue" ("scheduled")', None)
Traceback (most recent call last):
  File "test.py", line 39, in <module>
    Queue.create_table()
  File "/home/rcarmo/codebits/peewee.py", line 1969, in create_table
    db.create_index(cls, fields, unique)
  File "/home/rcarmo/codebits/peewee.py", line 1548, in create_index
    field_objs = [model_class._meta.fields[f] if isinstance(f, basestring) else f for f in fields]
KeyError: 'm'
```

Any idea on what I'm doing wrong?


It's just a little thing:

``` python
(('mac_address'), False),
```

should be:

``` python
(('mac_address',), False), # <-- missing comma inside parentheses makes act like a string, not tuple
```


Thanks.

Maybe you should raise a custom exception when that happens, to avoid confusion?

On 16/10/2012, at 16:06, Charles Leifer notifications@github.com wrote:

> It's just a little thing:
> 
> (('mac_address'), False),
> should be:
> 
> (('mac_address',), False), # <-- missing comma inside parentheses makes act like a string, not tuple
> —
> Reply to this email directly or view it on GitHub.
Issue: Can't assign empty list on init
Author: JCB-K
State: closed
Created: 2012-10-14 15:46:22

I'm trying to make an attribute of a class an empty list on init, but I get the following error:

```
  File "~/.virtualenv/foobar/lib/python2.7/site-packages/peewee.py", line 281, in __set__
    instance._data[self.att_name] = value
TypeError: 'NoneType' object does not support item assignment
```

Steps to reproduce:

```
class BaseModel(Model):
    class Meta:
        database = database

class Foo(BaseModel):
    bar = CharField()

    def __init__(self):
        self.list = []

x = Foo()
```


Peewee models perform initialization on startup.  Call the `super` method first:

``` python

def __init__(self):
    super(Foo, self).__init__()
    self.list = []
```