akumoth/peewee-issues huggingface dataset:
Issue: Hug framework support
Author: 0gk
State: closed
Created: 2017-01-16 20:20:20

Please, add section about Hug support. I try to use instructions for Falcon, but it don't work. May be Hug just have no hooks or smth like?

I'd accept a PR with docs.

Maybe something like this:

```
@hug.request_middleware()
def preProcessing(request, response):
    db.connect()

@hug.response_middleware()
def afterProcessing(request, response, resource):
    db.close()
```
Issue: why there is no pskel?
Author: axiaoxin
State: closed
Created: 2017-01-13 08:43:51

I read the doc: <http://docs.peewee-orm.com/en/latest/peewee/database.html#generating-skeleton-code>, I test using pskel to generate the skeleton code, but I found the pskel is not exists in my local

under the site-packages, peewee's install-files.txt is:

```
..\peewee.py
..\pwiz.py
..\playhouse\apsw_ext.py
..\playhouse\berkeleydb.py
..\playhouse\csv_loader.py
..\playhouse\csv_utils.py
..\playhouse\dataset.py
..\playhouse\db_url.py
..\playhouse\djpeewee.py
..\playhouse\fields.py
..\playhouse\flask_utils.py
..\playhouse\gfk.py
..\playhouse\hybrid.py
..\playhouse\kv.py
..\playhouse\migrate.py
..\playhouse\pool.py
..\playhouse\postgres_ext.py
..\playhouse\read_slave.py
..\playhouse\reflection.py
..\playhouse\shortcuts.py
..\playhouse\signals.py
..\playhouse\sqlcipher_ext.py
..\playhouse\sqliteq.py
..\playhouse\sqlite_ext.py
..\playhouse\sqlite_udf.py
..\playhouse\test_utils.py
..\playhouse\__init__.py
..\peewee.pyc
..\pwiz.pyc
..\playhouse\apsw_ext.pyc
..\playhouse\berkeleydb.pyc
..\playhouse\csv_loader.pyc
..\playhouse\csv_utils.pyc
..\playhouse\dataset.pyc
..\playhouse\db_url.pyc
..\playhouse\djpeewee.pyc
..\playhouse\fields.pyc
..\playhouse\flask_utils.pyc
..\playhouse\gfk.pyc
..\playhouse\hybrid.pyc
..\playhouse\kv.pyc
..\playhouse\migrate.pyc
..\playhouse\pool.pyc
..\playhouse\postgres_ext.pyc
..\playhouse\read_slave.pyc
..\playhouse\reflection.pyc
..\playhouse\shortcuts.pyc
..\playhouse\signals.pyc
..\playhouse\sqlcipher_ext.pyc
..\playhouse\sqliteq.pyc
..\playhouse\sqlite_ext.pyc
..\playhouse\sqlite_udf.pyc
..\playhouse\test_utils.pyc
..\playhouse\__init__.pyc
dependency_links.txt
PKG-INFO
SOURCES.txt
top_level.txt
..\..\..\Scripts\pwiz.py
```

It's only included in the git repo.

Actually `pskel` is declared usign the `script=[]` parameter of `setup.py` installation script, which it fails on many occasions to properly install things.

It would work out of the box in all python environments if it was converted into console entry-point:
http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point
Setuptools will generate an `.exe` on Windows, and guess correctly the path if python-interpreter on POSIX.

I can provide a pull-request, but it would require some minor changes:
- create new `main()` functions on `/pwiz.py` and `/playhouse/pskel`, 
- rename it as `pskel.py`.

Would that be ok?
Issue: Add view support to pwiz model generator
Author: toddhay
State: closed
Created: 2017-01-11 19:28:10

Please add the ability to migrate database views to a model using the pwiz model generator capability.   We're currently using postgresql and sqlite so those would be our initial database type requests.  

Thanks for the great peewee library, we use it heavily in our software development.

I do not have any plans for supporting this.
Issue: Validation for choices in the application layer
Author: ParthGandhi
State: closed
Created: 2017-01-10 16:04:40

I get why you don't validate for `choices` in the db layer.

Any reason it can't be implemented at the application layer? For backwards compatibility validated choices can be a new attribute called `allowed`/`validated_choices`? 

The behavior i'm thinking of is to throw a `ValueError` on saving a field if its not one of the allowed values. 

It can be validated in the application layer -- *your* application!

Heh, we already do that! Was wondering if there was any interest upstream.

Thanks!
Issue: UNION with 3 or more queries leads to invalid SQL syntax (MySQL)
Author: danygielow
State: closed
Created: 2017-01-09 12:09:40

With the MysqlDatabase a UNION with 3 or more queries leads to a wrong syntax by wrapping individual UNION pairs in parentheses.
I don't know if other databases are affected as well, SQLiteDatabase seems to work.

Test Case:
```python
class A(Model):
    id = PrimaryKeyField()
    b = IntegerField()

    class Meta(object):
        database = MySQLDatabase(MagicMock())

class TestPeeweeUnion(TestCase):
    def test_union(self):
        union = (A.select() | A.select() | A.select())

        self.assertEqual('(SELECT `t1`.`id`, `t1`.`b` FROM `a` AS t1) UNION (SELECT `t2`.`id`, `t2`.`b` FROM `a` AS t2) UNION (SELECT `t3`.`id`, `t3`.`b` FROM `a` AS t3)', union.sql()[0])
```

The assert fails because instead the following string is generated:

```sql
((SELECT `t1`.`id`, `t1`.`b` FROM `a` AS t1) UNION (SELECT `t2`.`id`, `t2`.`b` FROM `a` AS t2)) UNION (SELECT `t3`.`id`, `t3`.`b` FROM `a` AS t3)
```

The first pair of UNION is in parentheses.

Affected Versions are 2.8.4 and 2.8.5.

Thank you for looking into this.

Edit: Judging by the test TestCompoundSelectSQL.test_multiple_with_parentheses this behavior is apparently desired. So in MySQL the parantheses should be set differently to the default.

Fixed in d339c03. Thanks for the patch!
Issue: Prefetch prefetches all foreign keys.
Author: tkukushkin
State: closed
Created: 2017-01-07 22:29:33

I need to prefetch by only specific foreign key, but it prefetches by all.
Maybe need to add possibility to specify field for prefetch instead of join_model?

Also prefetch don't preserves by query methods like `Query.where` and it is not lazy. Maybe good to do query method like `Query.prefetch(self, *refs)` where `refs` – tuples like `(User.tweets, Tweet.select())` or simply `User.tweets`. This method will be lazy.

Sorry for my English 😢.

Unfortunately, prefetch cannot be lazy by definition. I'd suggest just writing the queries you need explicitly.
Issue: Sqlite closure calls cannot reference each other
Author: Naatan
State: closed
Created: 2017-01-06 20:10:26

When you do a call to `descendants` or `ancestors` the returned model holds an id value that that is the Closure model, this makes it impossible to use this data in followup queries.

Here's some code that illustrates the problem:

```
from peewee import *
from playhouse.apsw_ext import *
from playhouse.sqlite_ext import ClosureTable

db = APSWDatabase(':memory:')
db.load_extension("../env/lib/closure.so")

class Symbol(Model):
    
    parent = ForeignKeyField('self', index=True, null=True)
    name = CharField()
    
    class Meta:
        database = db
        only_save_dirty = True
        
SymbolClosure = ClosureTable(Symbol)

db.create_tables([Symbol, SymbolClosure], True)

symbol1 = Symbol.create(name="create_symbol_inherited_root")
symbol2 = Symbol.create(name="create_symbol_inherited_root", parent=symbol1.id)
symbol3 = Symbol.create(name="create_symbol_inherited_root", parent=symbol2.id)

child1 = SymbolClosure.descendants(symbol1,1).first()
print child1.id

child2 = SymbolClosure.descendants(child1,1).first()
print child2.id
```

The resulting output:

```
<peewee.SymbolClosure object at 0x7f85299a7990>
Traceback (most recent call last):
  File "repro.py", line 28, in <module>
    child2 = SymbolClosure.descendants(child1,1).first()
  File "/home/nathan/Projects/codeintel/env/lib/python2.7/site-packages/peewee.py", line 3114, in first
    return self.peek(n=n)
  File "/home/nathan/Projects/codeintel/env/lib/python2.7/site-packages/peewee.py", line 3104, in peek
    res = self.execute()
  File "/home/nathan/Projects/codeintel/env/lib/python2.7/site-packages/peewee.py", line 3149, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/home/nathan/Projects/codeintel/env/lib/python2.7/site-packages/peewee.py", line 2833, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/home/nathan/Projects/codeintel/env/lib/python2.7/site-packages/playhouse/apsw_ext.py", line 116, in execute_sql
    self._execute_sql(cursor, sql, params)
  File "/home/nathan/Projects/codeintel/env/lib/python2.7/site-packages/playhouse/apsw_ext.py", line 109, in _execute_sql
    cursor.execute(sql, params or ())
TypeError: Bad binding argument type supplied - argument #1: type SymbolClosure
```

Note the first print (of `child.id`) is returning an instance of `peewee.SymbolClosure` rather than the actual id. This is the root of the problem.

Anyone? I've been digging on this for a while myself now, but there is so much abstraction it's hard to figure out how to properly fix this.

Looking at this now, sorry for the delays!

Fixed -- Peewee was trying to be too clever by resolving the JOIN-ed closure table to a model instance and patching it to the place it was joined (the source table's primary key).
Issue: Dicts not resolving join column name conflicts properly
Author: jontonsoup
State: closed
Created: 2017-01-05 17:36:05

Say I want to join three tables and two of the tables have the same column. 
```
class Order(Model):
     status = Text....
class Betcycle(Model):
      status = Text....
```
```
 rt_orders = list(Order.select(Order, RtPick, Betcycle).join(
     RtPick).join(Betcycle).where(Order.status == "SETTLED").dicts())
 df_rt_orders = pd.DataFrame(rt_orders)  ##use dict 
```

The generated SQL will be correct (and will correctly call from the right table), but the retuning dict will only pick one of the `status` columns instead of alias them properly by table. 

I can do this: 

```
Order.select(Order, Order.status.alias("order_status"), RtPick, Betcycle.status.alias("betcycle_status")).....
```

but it seems like this can, and should be something that is automatically resolved. 

I can try to submit a pull request if you agree this is a problem


How would it be automatically resolved? What would happen? How would Peewee pick one?

The answer you've come up with -- specifying an alias -- is the right approach.

I'm not sure why you couldn't automatically prepend the name of the model infront of the column name and return both in cases where there is a clash.

I've seen this in other ORMs I've used. The current behavior is dangerous because you don't know which column you are getting back and there are no warnings emitted. It fails silently. 

What do you think? 

On Mon, Jan 09, 2017 at 4:25 PM Charles Leifer

<
mailto:Charles Leifer <notifications@github.com>
> wrote:

a, pre, code, a:link, body { word-wrap: break-word !important; }

Closed
https://github.com/coleifer/peewee/issues/1163
.

—

You are receiving this because you authored the thread.

Reply to this email directly,
https://github.com/coleifer/peewee/issues/1163#event-916600137
, or
https://github.com/notifications/unsubscribe-auth/AAznRLh5vOf5cpz7cudDbBGElQciGeUEks5rQqXAgaJpZM4Lb76I
.
Issue: Cascade delete is not working as expected
Author: aellwein
State: closed
Created: 2017-01-05 15:01:28

Hi Charles,

i created a table which is related to another table by a ForeignKeyField and on='CASCADE' policy in Sqlite.
It seems like if i delete a record from the parent table, the children are not deleted.
Here is a test that proves that behavior:
https://gist.github.com/aellwein/9e814769196e5524e94199163aa4a0bd

Am i doing something wrong?
Thanks!
Alex


Looks like sqlite by default has foreign key support turned off:

http://stackoverflow.com/a/13641365/724251

Need to add something like:

```python
Tweet.raw("PRAGMA foreign_keys=ON").execute()
```

Might be a nicer way included somewhere in the peewee docs.

@alexlatchford 
thanks for pointing it out. 
Seems to be mentioned in the documentation, in http://docs.peewee-orm.com/en/latest/peewee/database.html?#additional-connection-initialization

Although imho it is a little bit scary, that referential integrity is turned off by default.


@aellwein -- don't use SQLite if you don't know how it works. Simple as that.
Issue: Add a new features for sql output.
Author: b0123456789
State: closed
Created: 2017-01-04 08:22:33

I use sql function to get a sql output,but i found that it display this **"('SELECT `t1`.`al_id`, `t1`.`al_uid` FROM `wyx_account_live` AS t1 WHERE ((`t1`.`al_id` > %s) AND (`t1`.`al_platform_type` = %s)) ORDER BY `t1`.`al_id` LIMIT 1', [31, 1])"**.but i hope it output this **"SELECT `t1`.`al_id`, `t1`.`al_uid` FROM `wyx_account_live` AS t1 WHERE ((`t1`.`al_id` > 31) AND (`t1`.`al_platform_type` = 1)) ORDER BY `t1`.`al_id` LIMIT 1"** to excute for mysql gui client to find out errers.  Each time i should  wirte  **print(sql[0] % tuple(sql[1]))**,it really unfriendly.  Is there a function? or can you add a function like that.
Issue: create_or_get() uses AND instead of OR
Author: tarashypka
State: closed
Created: 2017-01-02 22:49:52

Currently, instead of
```
next_with_id, new = ProductCategory.create_or_get(**as_dict(next))
next.id = next_with_id.id
next = next.parent if new else None
```      
I am doing
```
try:
    with db.atomic():
        next.save()
    next = next.parent
except IntegrityError:
    next = ProductCategory.get(
        (ProductCategory.id == next.id) |  # primary key
        (ProductCategory.name == next.name) |  # unique field 1
        (ProductCategory.cat_id == next.cat_id)  # unique field 2
    )
    next = None
```
since the first variant produces
> product.ProductCategoryDoesNotExist: Instance matching query does not exist:
> SQL: SELECT "t1"."id", "t1"."name", "t1"."cat_id", "t1"."n_products", "t1"."n_pages", "t1"."parent_id" FROM "product_category" AS t1 WHERE ((("t1"."name" = %s) AND ("t1"."cat_id" = %s)) AND ("t1"."id" IS %s))
> PARAMS: [u'Appliances', u'4044_90548', None]

Is there a particular reason why create_and_get() uses AND operator instead of OR operator?
Because it could have been much easier to get documents by their unique fields yet without knowing their actual id values.

Looks like the 'get' part in 'create_or_get' uses 'AND' because at that point you would be trying to get a specific record, not a record that looks like the supplied parameters.

http://docs.peewee-orm.com/en/latest/peewee/querying.html#create-or-get

The same is true for 'get_or_create', you want that specific record, not something that looks like it, and if it couldn't be found, create it.

That said, imagine if it used 'OR' instead and I was, for example, looking for a certain resident belonging to a certain address.

Resident.get((Resident.address == '123 Fake Street') | (Resident.name == 'John'))

These methods use `and` because the Django APIs they are intended to mirror use that behavior. This isn't mongo.
Issue: Join Query Scopes
Author: jontonsoup
State: closed
Created: 2016-12-29 15:39:03

Hi!

I'm trying to put a where statement in a model class and use that in a relation as a scope. 

```
class Pizza(Model):
    brand = ... blah blah
    
    @classmethod
    def where_cheddar(cls):
         return cls.toppings.where(Topping.type == "CHEDDAR") #HERE IS AN ERROR / CIRCULAR IMPORT

class Topping(Model):
    pizza = ForeignKeyField(Pizza, related_name="toppings")
    type = Text.... 

    def where_cheddar(self):
        return self.where(Topping.type = "CHEDDAR")

```

There are two ways I'd like to be able to do this: The first is less DRY but is the circular import above. 

If I skip the circular import and write this using the instance method for topping: 

```
pizza = Pizza.select().first()
pizza.toppings.where_cheddar()
```

I get a 'SelectQuery' object has no attribute where_cheddar error because the foreign key field relation returns a select query and where_cheddar operates on the Topping class. 

I'm sure there is a way to do this, but I must be missing something. 

Any help would be appreciated. Thanks! 

**EDIT:**

I found model_class on SelectQuery, but this feels dirty. 

I would have to do:

```
 return cls.toppings.where( cls.toppings.model_class.type == "CHEDDAR") 

```

It violates the encapsulation of the Topping class.

As you said, your issue is a circular import. That's not really an issue peewee is designed to solve. In OO terms you probably need to add a third class that can read both modules and then route your access through that.

@coleifer Thanks for taking a look at this!

Are you familiar with active record? What I'm talking about is pretty common and is called an association scope.

Examples: 
```
belongs_to :firm, -> { where(id: 2) }
belongs_to :user, -> { joins(:friends) }
belongs_to :level, ->(level) { where("game_level > ?", level.current) }
```

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Customizing+the+query

The way peewee seems to be designed makes it impossible to put an association scope in the class, but I'm really hoping that I am wrong here. 

Does what I'm shooting for make more sense now?
Issue: multi-table join fail
Author: argpass
State: closed
Created: 2016-12-26 07:47:24

The `Car`:
![image](https://cloud.githubusercontent.com/assets/16532113/21478024/78359ffe-cb82-11e6-88b8-bdcb68e9c32e.png)

The `Device`:
![image](https://cloud.githubusercontent.com/assets/16532113/21478012/623bedf2-cb82-11e6-9c35-729cd3713a4d.png)

The `CarTerminal`:
![image](https://cloud.githubusercontent.com/assets/16532113/21477997/3946bb3e-cb82-11e6-97bc-cffbbb4a599e.png)

here is right:
![image](https://cloud.githubusercontent.com/assets/16532113/21477992/2a728ebc-cb82-11e6-9a8f-91dd140d6ccb.png)
![image](https://cloud.githubusercontent.com/assets/16532113/21477940/98b6e7e8-cb81-11e6-8a94-a4d46c0c1eba.png)

here is wrong:
![image](https://cloud.githubusercontent.com/assets/16532113/21478078/f2e3e4f4-cb82-11e6-9eed-df79940378ef.png)
![image](https://cloud.githubusercontent.com/assets/16532113/21478076/ea727c18-cb82-11e6-98d0-38291e1af3bf.png)

here is also wrong, the sql is almost right without the fabricated `t2.id` ( CarTerminal has no field named `id` in fact ):
![image](https://cloud.githubusercontent.com/assets/16532113/21478253/6020ade4-cb84-11e6-9bb1-7944a68f5074.png)
![image](https://cloud.githubusercontent.com/assets/16532113/21478250/564fd772-cb84-11e6-96e9-588b6b169ceb.png)




Peewee uses a concept called the "query context" when building joins. The model being selected, or the last model that was joined, is considered the context. A subsequent call to ".join()" will be from the query context to the model indicated by the call to join().

I think you just need to add the appropriate ".switch(ModelName)" in between your joins to ensure that you're setting the appropriate query context. http://docs.peewee-orm.com/en/latest/peewee/api.html#Query.join

Also, why aren't you using any ForeignKeyFields?!
Issue: insert_many is prohibitively slow. Provide options for fast execution.
Author: bw2
State: closed
Created: 2016-12-23 16:59:14

I haven't looked into the cause in detail, but for a simple table with ~10 fields, running insert_many on batches of 1000 takes several times longer to run (even with validate_fields=False) than if I generate the sql query myself and run it with db.execute_sql.
I can provide more details if needed, but if the reason is clear, it would be great to have additional args for insert_many that disable whatever extra steps it's doing and bring execution time down to the level of just concatenating strings and then running execute_sql. 


I'm sorry you're finding it slow. The reasons for this will be apparent if you look into what the code is doing. There are quite a few small optimizations in there, but it'll never be close to as fast as just writing the SQL by hand and running it that way. Python's slow.
Issue: Error while Connecting using a Database URL
Author: sonam0211
State: closed
Created: 2016-12-23 13:43:17

app.py
```
app = Flask(__name__)
bootstrap = Bootstrap(app)

def setup_db():
    create_model_tables([User, Notepad], fail_silently=True)

if __name__ == '__main__':
    setup_db()
    app.secret_key = 'super_secret_key'
    app.run(debug=True)
```


Model.py
```
import os

from peewee import Model, CharField, SqliteDatabase, CharField, ForeignKeyField, IntegrityError
from playhouse.db_url import connect

db = connect(os.environ.get('DATABASE_URL' or 'sqlite:///notes.db'))


class BaseModel(Model):
    class Meta:
        database = db


class User(BaseModel):
    ....


class Notepad(BaseModel):
    ....

```

Error

```
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from model import User, Notepad
  File "/Users/rahul/projects/personal/Online-Notes/model.py", line 33, in <module>
    db = connect(os.environ.get('DATABASE_URL' or 'sqlite:///notes.db'))
  File "/Users/rahul/.virtualenvs/Online-Notes/lib/python2.7/site-packages/playhouse/db_url.py", line 84, in connect
    parsed = urlparse(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urlparse.py", line 143, in urlparse
    tuple = urlsplit(url, scheme, allow_fragments)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urlparse.py", line 182, in urlsplit
    i = url.find(':')
AttributeError: 'NoneType' object has no attribute 'find'
```

Could it have something to do with your connect statement?

    db = connect(os.environ.get('DATABASE_URL' or 'sqlite:///notes.db'))

Should be:

    db = connect(os.environ.get('DATABASE_URL') or 'sqlite:///notes.db')

@CorverDevelopment has it.
Issue: Add support for ON CONFLICT DO NOTHING in Postgres.
Author: cassidykelly
State: closed
Created: 2016-12-21 00:06:06

I saw in the documentation there is a plan to add support for Postgres 9.5's ON CONFLICT DO UPDATE (i.e. upsert), but I didn't see any mention of ON CONFLICT DO NOTHING. This is one simple way to support the latter, although it does not support adding a **_conflict_target_**.

Will this get merged? Any obvious show-stoppers?

Full support for ON CONFLICT is pretty complex. Take a look at this: https://www.postgresql.org/docs/9.5/static/sql-insert.html

This feels like a stop-gap implementation. I'd prefer t be comprehensive in implementing this feature, so I'm closing this.
Issue: There is no ID field in my table why select will default with it
Author: amosnothing
State: closed
Created: 2016-12-19 13:36:57

# my code
class user_account(Model):

    user_no = BigIntegerField()
    amount = DecimalField()
    frozen_amount = DecimalField()
    create_time = DateTimeField()
    creator = CharField()
    edit_time = DateTimeField()
    editor = CharField()
    is_del = BooleanField()
    version = CharField()

    class Meta:
        database = db

db.connect()

print user_account.select()

-------------------------

then you will see:
<class '__main__.user_account'> SELECT **"t1"."id"**, "t1"."user_no", "t1"."amount", "t1"."frozen_amount", "t1"."create_time", "t1"."creator", "t1"."edit_time", "t1"."editor", "t1"."is_del", "t1"."version" FROM "user_account" AS t1 []

but there is no id fieid in my table.