akumoth/peewee-issues huggingface dataset:
Issue: pip install failed
Author: JoanEliot
State: closed
Created: 2019-09-23 14:31:51

(Windows 10, same experience with global/'main' Python and a venv) 
I'm a Python dunderhead so excuse any bad lingo or other evidence of lack.
 
Installation via pip/pypi gave no errors, but imports failed. Checking \site-packages I find peewee.py and pwiz.py out bare in the main folder, and an egg-info folder but no plain peewee folder. Installation via the peewee git/setup.py produced the expected peewee-3.11.0-py3.7 folder (well, .egg) and imports worked. 

Couldn't tell you. Peewee should be in the main folder, as it installs as a module, not a package.
Issue: Model.filter with alias behaves different from Model.select.where
Author: altunyurt
State: closed
Created: 2019-09-23 12:49:00

When alias is used with Model.filter, it somehow adds another table reference to queries. 


```
In [5]: oi = OrganizationInvitation.alias()

In [6]: oi.select().where(oi.invitee == "1212121212").sql()
Out[6]: 
('SELECT "t1"."id", "t1"."organization_id", "t1"."inviter_id", "t1"."invitee" FROM "organization_invitation" AS "t1" WHERE ("t1"."invitee" = ?)',
 ['1212121212'])

# Another table reference t2 is added here 
In [7]: oi.filter(oi.invitee == "1212121212").sql()
Out[7]: 
('SELECT "t1"."id", "t1"."organization_id", "t1"."inviter_id", "t1"."invitee" FROM "organization_invitation" AS "t1" WHERE ("t2"."invitee" = ?)',
 ['1212121212'])

In [8]: oi.filter(oi.invitee == "1212121212")
Out[8]: <peewee.ModelSelect at 0x7f56290ee748>

In [9]: oi.select().where(oi.invitee == "1212121212")
Out[9]: <peewee.ModelSelect at 0x7f5613fcceb8>

```

I'm using peewee 3.11.0 with apsw 3.28.0 on python 3.7.1

Why are you using an alias on a plain old select query? Aliases are designed for one reason: to allow you to reference the same table in multiple contexts within a query (e.g. a self-join). There is no reason to alias a model class simply to select from it -- just use the model itself.

Furthermore, you're using the `filter()` method incorrectly. Why are you passing an expression into the call to `filter()`? Filter is designed solely to provide a django-compatible way of constructing where clauses. You should just chain calls to `.where()` if you're passing in expressions:

```python

OI = OrganizationInvitation  # no alias!
q = OI.select().where(OI.invitee == '121212121')

# Or,
q = OI.select().filter(invite='1212121')  # Filter uses kwargs, *not* expressions.
```
Issue: Sybase Database Connection in Peewee.
Author: govind-savara
State: closed
Created: 2019-09-23 11:23:26

Hi, Developers.

Do we have an option to connect to Sybase Database using Peewee ORM?

No, but it's easy to add your own database backed.

You'll probably want to find a db-api 2.0 driver and then read the docs on adding a new database driver:

http://docs.peewee-orm.com/en/latest/peewee/database.html#adding-a-new-database-driver
Issue: AttributeError: 'bool' object has no attribute 'safe_name' after upgrade 3.10 -> 3.11
Author: grandchild
State: closed
Created: 2019-09-23 08:36:37

I updated to peewee to `3.11.0` from `3.10.0` 4 days ago and it presented with the following error. I cannot right now get more detail, because I don't have the time. But I will provide a minimal program reproducing the bug tomorrow. This is just meant as a placeholder, and maybe it'll already point you into the right direction. For now, I downgraded peewee to `3.10.0` and all is well again.

<details><summary>traceback</summary>

<pre>
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/[...]/app/api.py", line 97, in people
    & (EasyContacts.type_id == ContactType.PERSON)
  File "/home/[...]/app/api.py", line 88, in <listcomp>
    c.referenced_to
  File "/usr/lib/python3.7/site-packages/peewee.py", line 4221, in next
    self.cursor_wrapper.iterate()
  File "/usr/lib/python3.7/site-packages/peewee.py", line 4140, in iterate
    result = self.process_row(row)
  File "/usr/lib/python3.7/site-packages/peewee.py", line 7364, in process_row
    if instance not in set_keys and dest not in set_keys \
  File "/usr/lib/python3.7/site-packages/peewee.py", line 6403, in __hash__
    return hash((self.__class__, self._pk))
  File "/usr/lib/python3.7/site-packages/peewee.py", line 6294, in get_id
    return getattr(self, self._meta.primary_key.safe_name)
AttributeError: 'bool' object has no attribute 'safe_name'
</pre>
</details>

Wow, thanks! So impressed by the quality of your work in general. Thank you for what you do!

Pushed 3.11.1 as this seemed like a potentially disruptive regression. Thx for reporting.
Issue: Is there a way to specify postgresql schema at run time?
Author: shon
State: closed
Created: 2019-09-20 13:00:28

Is there any way to specify schema at runtime?
This is specific to postgresql.

**Pseudo code**

```python
class Post(Model):
     title = TextField()
     
    class Meta:
        def schema_name():
              return 'site' + appcontext.tenant_id

```

```python
appcontext.tenant = 13
Post.get(Post.title == 'thanks charles')
# sql: select * from site13.post
```
**Some context**: I am trying to evaluate with different approaches to design a multi-tenant app using postgresql+peewee

Sorry, peewee doesn't support that. I'd suggest writing a context manager that explicitly sets `Model._meta.schema`.

Untested, but possibly something like this (could be optimized):

```python

from contextlib import contextmanager

@contextmanager
def with_schema(models, schema):
    orig = []
    for model in models:
        orig.append(model._meta.schema)
        model._meta.schema = schema
        G = model._meta.model_graph(refs=True, backrefs=True)  # Traverse all fks.
        for _, rel_model, _ in G:
            rel_model._meta.schema = schema
    yield
    for model, schema in zip(models, orig):
        model._meta.schema = schema
        G = model._meta.model_graph(refs=True, backrefs=True)
        for _, rel_model, _ in G:
            rel_model._meta.schema = schema
```
Issue: 'autoconnect' unused in 'PostgresqlExtDatabase'
Author: wibimaster
State: closed
Created: 2019-09-19 09:19:09

Hi !

I'm using Peewee for a Postgres database connection.

The latest documentation says "If you would like to use these awesome features, use the PostgresqlExtDatabase from the playhouse.postgres_ext module", so.. That's what I did :D

But I need the "autoconnect=False" feature, to handle the connection on my own and avoid timeout issues (Peewee never reconnects on its own).

I tried and see that this feature works with "PostgresqlDatabase", but not "PostgresqlExtDatabase" from playhouse..

Is it an oversight or a will?

Thanks !

Nice find. I need to fix, thanks.
Issue: Update peewee.py
Author: lvhaidong
State: closed
Created: 2019-09-19 02:31:17

Add field comment info

Nope. That's a mysql-only thing.

How to set comment in the model, can be described in the database can see the details

It is not supported by sqlite or postgres.

I am use mysql, which field can be described in the database can see the details

Are you averse to adding dv-specific features?

> Are you averse to adding dv-specific features?

Not at all, but they have to be added in such a way that they are compatible. This patch would break hard if a user added help_text on sqlite or postgres. That's not compatible. There are *many* examples of compatibility in peewee -- on conflict handling, window function handling, data-types, etc, etc.
Issue: New field type with custom sql conversion
Author: tclarke
State: closed
Created: 2019-09-18 19:16:36

I'm using trunk and some coffee I had running with the latest 2.x from pypi is no longer working. I'm implementing a Field for postgis point geometry. My conversation function to python used
```
q=SelectQuery(self.model_type, fn.ST_X(value).alias('x'), fn.ST_Y(value).alias('y'))
r=q.first_row()
return r.x, r.y
```
I can't get your working now. Is there an equivalent? Or a better way? If prefer to have the initial select do the conversation instead of a second select query.

Bro are you high? Do you english?

my phone apparently autocorrected a bunch...nice way to build a community....

This might help: https://github.com/coleifer/peewee/blob/master/tests/fields.py#L1134-L1167

For those running across this, I extrapolated from the link above. The fix is:
```
r = Select(None, [fn.ST_X(value).alias('x'), fn.ST_Y(value).alias('y')]).first(self.model._meta.database)
return r['x'], r['y']
```

I would not recommend that, as it implies a query for every value. I'm not sure where or in what context you are executing this additional query.

It'd be better to, whenever selecting from this field, to do the conversion automatically. This is where the `db_value()` method comes into play, which is somewhat exemplified in the code I linked.

I may have a misunderstanding of what `db_value()` is doing. I thought that it converted values for storing in the database? And `python_value()` converted database values to python values? I'm doing the above in `python_value()` as the representation of the geometry column as returned by SELECT is WKB. The appropriate SELECT to get the components of the field would be something like `SELECT ST_X(bbox),ST_Y(bbox) FROM tablename;` which returns 2 entries for a single column.

What you're doing is super inefficient, is what I'm saying. If you run a query that returns 100 rows, you're sending off 100 *additional* queries. Nobody should do this, it's terrible advice and inefficient.

Another route is to modify your select so that you grab the values explicitly:

```python

query = MyModel.select(
    MyModel, 
    fn.ST_X(MyModel.bbox).alias('bbox_x'),
    fn.ST_Y(MyModel.bbox).alias('bbox_y'))
```

I understand that it's inefficient, I'm trying to figure out how to make the ORM do it in a single select and automatically populate the object. The context is something like:
`
class Bar(Model):
  id = IntegerField(primary_key=True)

class Foo(Model):
  bbox = BoxField()  # this is the custom field with the bbox accessor
  bar = ForeignKey(Bar, backref="foo")

# ...
tmp = Bar.get(id=my_bar_id)
json.dumps(model_to_dict(tmp))
Issue: Peewee Decimal Field Max Digits
Author: MatthewDLove
State: closed
Created: 2019-09-18 14:30:32

Hello, 

Thank you for this great ORM. I work at a crypto start-up and it's been very useful so far. 

We need to store values of monetary transactions, so we implemented a table using the decimal field (underlying table Postgres):

class Order(CustomModel):
    ....
    price = DecimalField(max_digits=15)
    execution_price = DecimalField(max_digits=15, null=True)
    ....

However, when our tests make an order with price 100,000 (crypto is crazy), we get this error:

"numeric field overflow
DETAIL:  A field with precision 10, scale 5 must round to an absolute value less than 10^5"

But max_digits 15 should set the precision to 15, correct? When I read the code I linked below, it doesn't seem like passing in this parameter is doing anything:

https://github.com/coleifer/peewee/blob/00f32293e99bc7ad90f4706bd6b46d88f200b431/peewee.py#L4491

According to this, postgres should be able to store far more than 6 digits before the decimal place:

http://www.postgresqltutorial.com/postgresql-numeric/

So is this an actual issue, or am I misreading something? I hope this is actually worth you reviewing and not a stupid mistake on my part - I see how active you are online answering questions, and I both appreciate and admire that. 

Max digits corresponds to precision
Decimal places corresponds to scale

I'm not clear in your example whether you're using the "," as a thousands-separator or a decimal separator.

If unspecified, scale defaults to 5. So your example would be equivalent to:

```
"price" NUMERIC(15, 5) NOT NULL
```

I have absolutely no issues with this in a simple test-case:

```python

class R(Model):
    val = DecimalField(max_digits=15)

R.create(val=Decimal('100000.000'))  # val='100000.000' also works fine.
r = R.get()
print(r.val)  # 100000.00000
```

SQL for the table:

```sql
CREATE TABLE IF NOT EXISTS "r" (
  "id" SERIAL NOT NULL PRIMARY KEY,
  "val" NUMERIC(15, 5) NOT NULL)
```

Something is wrong with your schema or the code you've shared, but it appears working fine for me.

> crypto startup

not even once
Issue: Union broken in MySQL due to parentheses problem
Author: jakedt
State: closed
Created: 2019-09-16 21:10:35

I believe that https://github.com/coleifer/peewee/commit/36bd887ac07647c60dfebe610b34efabec675706 broke a weird query that we have. I believe that the outer level or parens are not being detected.

Work in 3.6.4
Broken in 3.10.0

Repro case:
```python
import logging

from peewee import *
from playhouse.db_url import connect

db = connect("mysql://user:password@localhost:3307/testdb")

class Person(Model):
  name = CharField()

  class Meta:
    database = db # This model uses the "people.db" database.


if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  db.create_tables([Person])
  for person in (Person
                 .select()
                 .where(Person.id << (Person
                                      .select(Person.name)
                                      .where(Person.name == "bill")
                                      .union_all(Person
                                                 .select(Person.name)
                                                 .where(Person.name == "frank")
                                                )
                                     )
                       )
                ):
    print(person.name)
```

Yeah, this query is failing on MySQL but works OK for Sqlite or Postgres.

For sqlite, we have a rule to never put parentheses around the left- or right-hand side.

For postgres, we always do, since postgres handles it correctly.

For mysql, we have a rule to not put parentheses if the left- or right-hand side is itself a compound query. It appears this is overly-simplistic. I honestly don't understand how mysql decides when to handle parentheses vs choke/throw an error. I think I've hacked in a fix for now by checking if the compound query is inside an expression or function-call, and in that case to avoid using parens. If there's a better rule I can apply, I'd be eager to implement it.

It appears that this particular sql parsing bug has existed in MySQL for over 12 years now:

https://bugs.mysql.com/bug.php?id=25734

It appears to be fixed in 8.0, but I'm guessing you're either on 5.x or using MariaDB.

My suggestion is that although this is somewhat fixed, to try and re-work your query.

MariaDB thought they fixed it for 10.4: https://jira.mariadb.org/browse/MDEV-11953

But they missed some scenarios, including using compound select queries inside an IN expression (e.g.). I've opened a ticket, so we'll see what happens: https://jira.mariadb.org/browse/MDEV-20606
Issue: chalice with peewee
Author: jlpl1999
State: closed
Created: 2019-09-16 01:18:55

Do you have any suggestion for chalice framework to open close connect with MySql
only at https request occurred? 
thanks 

So I took a quick look. You can't really rely on state or local memory being preserved between any two requests w/a lambda function, so I'm not sure if connection pooling makes sense in that environment (though you could try it and measure).

I'd just wrap the `route()` function to add a decorator that opens and closes the db on each request.

```python

app = Chalice(...)
db = MySQLDatabase(...)
# Or,
# db = PooledMySQLDatabase(...)

def my_route(*args, **kwargs):
    def inner(fn):
        def deco(*fa, **fk):
            db.connect()
            try:
                return fn(*fa, **fk)
            finally:
                db.close()
        return app.route(*args, **kwargs)(deco)
    return inner

@my_route('/foo/', ...)
def some_view(...):
    # db should be opened before this function is called
    # and will be closed afterwards
```

Something like that -- I didn't test it, but hopefully you get the idea.
Issue: How do I install the C-extensions? 
Author: amjith
State: closed
Created: 2019-09-11 22:25:12

The installation instruction mentioned that if Ihave cython installed then installing peewee will compile the C-extensions. But I'm not able to achieve taht result. 

First I installed Cython and then I pip installed peewee and tried to use the `CSqliteExtDatabase` but it is not installed.

I have copied the output from the steps below:

```
(tempenv-50a4169627007) ↪ pip freeze
Cython==0.29.13


(tempenv-50a4169627007) ↪ pip --no-cache-dir install -U peewee
Looking in indexes: https://pypi.python.org/simple
Collecting peewee
  Downloading https://files.pythonhosted.org/packages/a5/80/c84b0fa212a003db91c87822fd8559fed78e214c3c8f1a97a095ee711a95/peewee-3.10.0.tar.gz (2.3MB)
     |████████████████████████████████| 2.3MB 482kB/s 
Building wheels for collected packages: peewee
  Building wheel for peewee (setup.py) ... done
  Created wheel for peewee: filename=peewee-3.10.0-cp37-none-any.whl size=119321 sha256=4871885a538c6ab3ab7d923aeec56b3018062fa9d9da8acd81264831103d5932
  Stored in directory: /tmp/pip-ephem-wheel-cache-0i30f038/wheels/5c/bb/b7/365edb46082a8da5c202eb4dc86ae335e4b322e6b1d430dbc8
Successfully built peewee
Installing collected packages: peewee
Successfully installed peewee-3.10.0

(tempenv-50a4169627007) ↪ python
Python 3.7.4 (default, Jul  9 2019, 15:11:16) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import playhouse.sqlite_ext 
>>> playhouse.sqlite_ext.CYTHON_SQLITE_EXTENSIONS
False
>>> 
```

I don't have sqlite3.h. I'll try to get that before trying again. 

Yup. That was it. :man_facepalming: 

The `setup.py` should print an error if you run `setup.py build_ext`...looks like maybe it got swallowed by `pip`? Not sure. At any rate, glad you got it sorted.
Issue: Question: how to export SQL which can be used in postgreSQL
Author: trim21
State: closed
Created: 2019-09-11 15:45:00

In using peewee to generate some SQL, but postgreSQL database use `%s` as a place holder which is not supported by postgresql server, any chance I get postgresql compatible SQL string (which use `%#` as placeholder?

Is it possible now?

If it's now supported now, any plan for it?

Thank you very much.

seems that driver handle (escape) parameters itself instead of execute it as parameters in postgresql. Why doing that......

Peewee follows the DB-API convention / interface for executing queries. The SQL query uses placeholders for values, which are then interpolated by the driver however that needs to happen. Postgres (psycopg2) implements escaping as part of the library. Sqlite, however, prepares/compiles the statement *with* the placeholders, then binds the values separately using the `sqlite3_bind_XXX` interfaces. So it's up to the driver to do the appropriate thing for the database.

This is done for:

1. security. If you are binding your parameters, then you do not need to worry about sql injection attacks.
2. performance. Sqlite, for example, can reuse a compiled statement with a different set of parameters, avoiding the need to compile a statement over-and-over when only the parameters have changed.

In fact I want to use `asyncpg` to execute my peewee query asynchronous in my asyncio web server, which send query and parameters directly to postgresql server instead of escape it by python. So it only accepts query with `$1`,`$2` as placeholder. And simply custom `Context.param` won't work in this situation.

Did that mean that there is no workaround now to get Postgresql compatible SQL string by `query.sql()`, only psycopg2 compatible? 

Which means I have to handle SQL string after peewee generated it, I'm also considering security issues
Issue: OperationalError on iregexp
Author: kotofey-da-powah
State: closed
Created: 2019-09-11 07:25:25

Have a query that works fine:
```
groups = Group.select().where((Group.codename.regexp(q)) | 
(Group.study_program.regexp(q)))
```
But the 'iregexp' one is giving an sql operational error:
```
groups = Group.select().where((Group.codename.iregexp(q)) | 
(Group.study_program.iregexp(q)))
```
Traceback:
```
Traceback (most recent call last):
  File "/Users/kotofey/Documents/SPBUBot/vbio/handlers/longpoll.py", line 46, in run
    self.bot.process_message(update['object'])
  File "/Users/kotofey/Documents/SPBUBot/vbio/bot.py", line 100, in process_message
    handler['function'](msg)
  File "bot.py", line 43, in handle_message
    group_list(user, m, keyboard)
  File "bot.py", line 82, in group_list
    if len(group_list) == 0:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 1897, in __len__
    self._ensure_execution()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 1879, in _ensure_execution
    self.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 1796, in inner
    return method(self, database, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 1867, in execute
    return self._execute(database)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 2038, in _execute
    cursor = database.execute(self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 2994, in execute
    return self.execute_sql(sql, params, commit=commit)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 2988, in execute_sql
    self.commit()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 2759, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 183, in reraise
    raise value.with_traceback(tb)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/peewee.py", line 2981, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: near "IREGEXP": syntax error
```

Your database (sqlite) doesn't support IREGEXP. Just use a normal regexp with case-insensitive matches/captures.