akumoth/peewee-issues huggingface dataset:
Issue: OSS License compatibility question
Author: Ashley123456789
State: closed
Created: 2023-01-14 06:21:09

There’s some possible confusion on the license of your repository when you combine other open-source code. 

The module `tests/libs/mock.py` claims its license as **the BSD License**. However, the license of your whole project is shown as **the MIT license** in LICENSE, i.e., **less strict** than the BSD License on license terms, which has impacted the whole license compatibility in your repository and may bring legal and financial risks.

You can select another proper license for your repository, or write a custom license with license exceptions if some license terms couldn’t be summed up consistently
Issue: Unable to import module 'app': No module named 'playhouse'
Author: bitfede
State: closed
Created: 2023-01-04 11:36:10

Hello,

I am working on an [AWS Chalice](https://aws.github.io/chalice/quickstart.html#deploying) project, and I chose peewee as the ORM for the postgresql DB we have.

When I run things locally with `chalice local` I can run my api with no problem, I'm able to CRUD normally.

When I deploy my microservice using `chalice deploy` and I go test it out I encounter an error 500. When looking into the logs I see this message multiple times:

```
Traceback (most recent call last):
2023-01-03 22:39:57.665000 d87bac [ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'playhouse'
```

I cannot do `poetry add playhouse`, because I get the following error: `Could not find a matching version of package playhouse`. What can you suggest me to do to fix this?

Thank you so much

Peewee, the package, includes `playhouse` so I'd assume you would just need to ensure that `peewee` is installed in your environment and then `playhouse` should work. I am not familiar with chalice, and questions such as this are better suited to StackOverflow.

@bitfede Note that you can [deploy AWS Chalice into AWS Lambda using an S3 bucket as an intermediary for your requirements.](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html#gettingstarted-package-zip) Your requirements, presumably including peewee would be zipped and uploaded to S3 then loaded into Lambda on creation. Another [example with Terraform](https://developer.hashicorp.com/terraform/tutorials/aws/lambda-api-gateway).

@coleifer Thank you for getting back to me very fast, I posted the question on StackOverflow.

@Insighttful really appreciate your response. Can you elaborate a bit more on why uploading the zip to S3 then load it into Lambda might make peewee's playhouse work compared to running `chalice deploy`? Thank you so much.

@bitfede have you confirmed that **_peewee was actually installed_** as a requirement in the Lambda environment with the standard deploy? Chalice deployments with requirements over 10mb can use S3 as an intermediary to load requirements into Lambda.
Issue: Error when aurora cluster instance failover
Author: oxeye-schmil
State: closed
Created: 2022-12-29 11:31:08

Hi,

I'm using PostgresqlDatabase to manage my Aurora DB.
I've encountered the following error after the the AZ on which the writer instance went down and caused a failover.
```
Traceback (most recent call last):
  File "/Users/schmil/work/oxeye-cloud/data_pipeline/sink_instance_creator/sink_instance_persistant_storage.py", line 72, in _handle
    query.execute()
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 1911, in inner
    return method(self, database, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 1982, in execute
    return self._execute(database)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 2761, in _execute
    return super(Insert, self)._execute(database)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 2477, in _execute
    cursor = self.execute_returning(database)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 2484, in execute_returning
    cursor = database.execute(self)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 3173, in execute
    return self.execute_sql(sql, params, commit=commit)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 3167, in execute_sql
    self.commit()
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 2933, in __exit__
    reraise(new_type, new_type(exc_value, *exc_args), traceback)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 191, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 3158, in execute_sql
    cursor = self.cursor(commit)
  File "/usr/local/lib/python3.9/site-packages/peewee.py", line 3145, in cursor
    return self._state.conn.cursor()
peewee.InterfaceError: connection already closed
```
I managed to reproduce it and when checked the connection before executing the query using `is_closed()` it returned false but still got the error

I'm using a single connection without connection pool because I have only a single thread
How can configure peewee to handle failovers?

`is_closed()` refers to internal state of Peewee, not the underlying psycopg2/libpq connection. Psycopg2 does not really expose a good way to check the state, see this comment for some background explanation: https://github.com/coleifer/peewee/issues/2654#issuecomment-1325199936 - also note that handling failover by silently reconnecting may not be a good idea, as any uncommitted changes prior to the reconnect could be lost.

My suggestion is to decide where, in your application, it makes the most sense to handle these kinds of events - and code defensively (catching the `InterfaceError` possibly, although I would have expected one slightly different error **before** the `InterfaceError` occurs -- typically an OperationalError).
Issue: Suggestion: .is_ and .is_not query lookups
Author: frosthamster
State: closed
Created: 2022-12-27 10:50:26

It'd be great to have an option to write `is` and `is not` lookups via functions `Model.field.is_(value)` and not `Model.field >> value` which is unreadable, similar to `Model.field.in_(value)`. It's useful for writing queries against nullable boolean fields, like `Model.is_finished.is_not(True)`
I can submit a pr if you want that change too.

We use `.is_null()`: http://docs.peewee-orm.com/en/latest/peewee/api.html#ColumnBase

But we can use it only for generating `is null` and `it not null`, not for `is not true`
Issue: Suggestion: SQLiteDatabase option to disable automatic DB creation
Author: MrBillium
State: closed
Created: 2022-12-26 15:45:26

There does not appear to be a way to disable DB creation if not found.

e.g db = SqliteDatabase('mydb.db',  pragmas={'foreign_keys': 1}, **create_db=False**)


Why this could be helpful:

I just went down a rabbit hole moving  running code to an Apache server where I forgot to set **home=/var/www/myapp** in my apache .conf file.
The result was everything ran fine without errors but the DB in the same dir as the rest of the code was not found so an empty copy was created by Peewee in /home/<user> 
This led to troubleshooting why a fully functional db was not returning any data.

In most cases of deployment or migration the database should not NEED  be 'created' and preventing it from happening iwould immediately show up as an error as it should.






Going to pass
Issue: bulk_create() doesn't work with server-side default values 
Author: gondolio
State: closed
Created: 2022-12-19 05:58:06

It appears that bulk_create does not work with server-side default values specified in [constraint](https://docs.peewee-orm.com/en/latest/peewee/models.html?highlight=table%20generation#:~:text=can%20use%20the-,constraints,-parameter%20to%20specify). 

For example, with this model:
```
class MyModel(Modell):
    datetime = DateTimeField(constraints=[SQL("DEFAULT CURRENT_TIMESTAMP")])
    class Meta:
        database = my_postgresql_db
```

The following works:
`MyModel().save()`

whereas this fails:
```
MyModel.bulk_create([MyModel()])

# IntegrityError: null value in column "datetime" violates not-null constraint DETAIL:  Failing row contains (2, null).
```

Any suggestions on how to bulk create using server-side defaults?

Yes, this is because `bulk_create()` is explicit about inserting all fields. If you want finer-grained control, for (e.g.) fields w/server-side defaults, you should just use `insert_many()` and be explicit.

Got it - so first convert the objects to dicts (via `object.__data__`) and then feed it into `insert_many()`. Thx!
Issue: playhouse/_sqlite_udf.pyx: compilation error
Author: wedobetter
State: closed
Created: 2022-12-16 14:32:30

Peewee Version: 3.15.4
Python: 3.11.0
Cython: 3.0.0a11
Platform: MacOS M1 - arm64
How to reproduce: pip install peewee

```
Error compiling Cython file:
      ------------------------------------------------------------
      ...
              if self.ct == 0:
                  return None
              elif self.ct < 3:
                  return self.items[0]
              else:
                  return self.selectKth(self.ct / 2)
                                                ^
      ------------------------------------------------------------
      
      playhouse/_sqlite_udf.pyx:137:42: Cannot assign type 'double' to 'int'

```
Issue: Type support
Author: gp-slick-coder
State: closed
Created: 2022-12-10 18:12:21

Type support in VSCode would be nice.

![image](https://user-images.githubusercontent.com/1906169/206869371-b9555cef-e9d1-4d66-a27f-5fc61968aae8.png)


Hey, why was this closed? Is there already a solution for this?

I already did some research ;-) unfortunately, the creator of this nice ORM doesn't like type hints. Coming from Typescript, python does miss a nice modern ORM solution :-( 

You can create your own hints (I think they're called "stubs"?), but this particular question has been asked and answered several times since type hints were introduced.
Issue: Gotcha for folks using update
Author: brewerja
State: closed
Created: 2022-12-02 19:36:38

Speaking from recent experience, I made a mistake when attempting to update a single field on a model.

```python
flag = Flag.get_by_id(flag_id)
flag.update(active=False).execute()
```

Given that I was operating on an instance of the model, I assumed I'd be updating that field only on a single row. However, given that in Python you can call a classmethod on an instance variable, it updates ALL the flags in the database! I'm glad I wasn't using the delete classmethod by accident.

```python
class Test:
    @classmethod
    def hello(cls):
        print("CLASS HELLO")

if __name__ == '__main__':
    t = Test()
    t.hello()
    Test.hello()
```

I wish I had a good suggestion for how to help others not make this mistake (other than learning Python better 😢 ). I see you do have `flag.delete_instance()` in addition to `Flag.delete()`. Would an `update_instance` method help?

Unfortunately, I see no way to detect, from a classmethod call, whether it was the class or an instance that invoked it.

[update]

I would favor what you suggested back in 2015:

>Classmethods operate on the table. Instance methods operate on the rows. Class represents table, instance represents row. It is just an inconvenient fact of python's `classmethod` decorator that you can call classmethods from an instance. I could write a special decorator that prevented this, e.g. `classonlymethod` but I don't think it's worth the time and more Pythonic anyways.

_Originally posted by @coleifer in https://github.com/coleifer/peewee/issues/586#issuecomment-95960454_

>Would an update_instance method help?

The docs and examples are already quite clear. You update an instance by setting the attributes and using `save()`, as this is an active-record style ORM.

I think it would be worth a warning note in the docs, letting folks know that Python will happily allow them to update every row in a table from an instance. It's a simple mistake that can have big consequences.

I really appreciated all the [n+1 problem docs](https://github.com/coleifer/peewee/blob/master/docs/peewee/relationships.rst#avoiding-the-n1-problem).
Issue: [feature] Query tagging capability
Author: recipe
State: closed
Created: 2022-12-02 03:55:25

Hi @coleifer,
What do you think in general about providing a capability to add [tags for queries](https://cloud.google.com/sql/docs/postgres/using-query-insights#using-sqlcommenter:~:text=When%20you%20examine%20results%20in%20SQL%20database%20logs%2C%20they%20appear%20as%20follows)? Tagging SQL queries simplifies application troubleshooting and can be helpful for people who are using the Google Cloud SQL. Their Query Insights feature is able to parse tags and provide [application centric view](https://www.youtube.com/watch?v=qN7x3ngwz1o) to SRE. 


Since this is so google-specific, I'm not too inclined to add it at the present.
Issue: can't select self-children items
Author: 1fisher
State: closed
Created: 2022-11-30 07:55:55

``` Python
class A(BaseModel):
      parent = ForieginField('self', null=True, backref='children')


for item in A.select():
    print(item.children)
```

`item.children` is exposed as a select query, which is iterable. Do this:

```python
a = A.get(...)
for child in a.children:
    print(child)
```

Also post questions like this on stack overflow. Self-referential fk docs are here for your reference: http://docs.peewee-orm.com/en/latest/peewee/models.html#self-referential-foreign-keys
Issue: PostgresqlDatabase multithreading performance problem
Author: mohamin1995
State: closed
Created: 2022-11-29 17:27:28

Hello,
I have a postgresql db connection:

```
database = PostgresqlDatabase(config_server['database_name'],
                              **{'host': config_server['host'], 'port': config_server['port'],
                                 'user': config_server['user'],
                                 'password': config_server['password']
                                 })

```
and each thread(4 thread) runs the below function with different date_time parameter, we must run the below function 100 times:

```
def get_estimations_v1(date_time, db) -> pd.DataFrame:
    Estimation._meta.set_table_name('est_' + date_time.strftime("%Y%m%d"))

    try:
        db.connect(reuse_if_open=True)
        query = Estimation.select().where(
            Estimation.date_time.between(date_time - timedelta(minutes=1), date_time))

         estimations = pd.DataFrame(list(query.dicts()))
         return estimations

    except Exception as ce:
        logger.error('load of estimations failed for {}, {}', date_time, str(ce))
        return
    finally:
        db.close()
        return

```
but the performance is worse than sequential running. anyone have any ideas?


@coleifer 

>but the performance is worse than sequential running. anyone have any ideas?

Python uses a global interpreter lock so only 1 thread is ever executing python code. If your code is CPU-bound then, due to context-switching, you will waste time overall.

You can try using multiple processes to circumvent this issue.

Although my job is IO-bound(multi-threading is not a bad choice), I implemented the multi process version too but the problem still persist with peewee then I implemented the function using psycopg2 adapter and the performance became 4 times faster(with multi threading).
@coleifer 

Was it also 4x faster single-threaded with psycopg2?

No, single-threaded version of psycopg2 almost takes time equal to single-threaded peewee.

I made a little script that inserts 100K rows and queries it single-threaded, using 4 threads and 4 processes. The single-thread and 4-thread run about the same, but the multiprocess version runs in about half the time.

The psycopg2-only version is faster, but this is somewhat expected since it carries none of the python-side overhead. It is also marginally faster (10%) when run multi-threaded.

I profiled the code and it looks to me like the bulk of the time in peewee is spent generating sql and serializing result rows to model instances, which is to be expected.
Issue: When the connection is maintained, the query suddenly takes a lot of time one day
Author: liuhuanxg
State: closed
Created: 2022-11-23 02:30:17

I use pypy2.7, When I connect to MySQL, Because of the large amount of data, the paging query used took about 3 seconds to complete all queries at the beginning, but after about seven days, it took 80-90 seconds. When the service was restarted, it was restored. I don't know why, does anyone know?

That's quite odd, but I can't really speculate as to what might be going on. When you say "the service" are you referring to the database server, or the python application? What does the query look like that went from 3 seconds -> 90 seconds? I'm almost positive that something in the Peewee code wouldn't be affecting this. The behavior should be effectively "stateless" -- you execute the query and do whatever with the results -- if it was a Peewee issue I'd think it would either be always slow or always fast.

So my guess is either it's the db server, pypy, or perhaps something else that may be going on inside your application.

Going to close for now unless there is compelling evidence this is in fact a peewee issue, but please feel free to comment w/more details and I'll give it some thought.
Issue: Allow usage of __init_subclass__ in Models
Author: conqp
State: closed
Created: 2022-11-21 13:35:12

Currently, one cannot use `__init_subclass__()` on `Model`s.
This is useful, if one wants to create an abstract model to be implemented with different parameters.
Unfortunately `ModelBase` currently hands off all kwargs to the parent type `type` resulting in `TypeError`s:

```python
from peewee import Model


class MyTemplate(Model):

    def __init_subclass__(cls, *args, some_param: str, **kwargs):
        super().__init_subclass__(*args, **kwargs)

        if some_param is not None:
            cls.SOME_PARAM = some_param


class MyImplementation(MyTemplate, some_param='hello world'):
    pass
```
Resulting in:
```shell
$ python3 test.py
Traceback (most recent call last):
  File "/root/test.py", line 13, in <module>
    class MyImplementation(MyTemplate, some_param='hello world'):
TypeError: __new__() got an unexpected keyword argument 'some_param'
```


Darn it. This is already fixed in `3.15.4`. I was using `3.14.8` on that particular machine.
Sorry for the noise.

No problem at all, I only just released that fix, with thanks to @smac89 for the patch.
Issue: affairs exist concurrency Error
Author: DreamBizideal
State: closed
Created: 2022-11-21 10:40:40

![QQ截图20221121183253](https://user-images.githubusercontent.com/82210587/203028544-30f73d9f-3684-485f-90e5-eefee34f40a3.png)
`if __name__ == '__main__':
    t1 = threading.Thread(target=sell2)
    t2 = threading.Thread(target=sell2)

    t1.start()
    t2.start()

    t1.join()
    t2.join()`

sorry i don't speak english
我开启了两个线程 一个线程在被修改完成后 另一个线程查不到实时数据  只能查到修改之前的数据（操作都在with db.atomic() as txn:）
并且我没有rollback()

不使用 db.atomic()就正常查询到  但是我需要事务
添加了事务即使数据库修改过数据  在db.atomic()里进行查询也不会查到当前的数据 只会查到之前的




You need to re-fetch it or use update... returning (if your database supports it).
Issue: Build a database (SQLite) with peewee WITHOUT setting in the python code the "name" of the table fields
Author: rollobesson
State: closed
Created: 2022-11-16 10:41:53

Hi everyboby
Hope my english will be understable
I need to read a folder fulled of Excel file and to feed a sqlite data base: Yes EASY
In fact I want to make a tool :
I feed a txt file with the name of the folder, the cells (eg: A1, B6, ...) and the names of the datas (eg: firstname, lastname, ...)
I lunch a Peewee program which reads the txt file and the folder and build the database
The difficulty is on the Class(Model): 

class mytable(Model):
    dynamic_data[0]=CharField()    "#  here "first_name" is in dynamic_data[0] 
    class Meta:
        database = db

 CharField() refuses to make an objet with a dynamic name coming from my txt file 

Who has an solution ?

Thank you !

The easiest way to do this is using the `type()` builtin:

```python
class BaseModel(Model):
    class Meta:
        database = ...

fields = {
    'dynamic1': CharField(),
    'dynamic2': IntegerField(),
    ...
}

# Create a new class. Signature is (name, bases, attrs).
MyTable = type('MyTable', (BaseModel,), fields)
```

This produces:

```python
>>> MyTable._meta.fields
{'id': <AutoField: MyTable.id>,
 'dynamic1': <CharField: MyTable.dynamic1>,
 'dynamic2': <IntegerField: MyTable.dynamic2>}

>>> MyTable._meta.table_name]
'mytable'
```
Issue: Remove redundant wheel dep from pyproject.toml
Author: mgorny
State: closed
Created: 2022-11-12 06:22:32

Remove the redundant `wheel` dependency, as it is added by the backend automatically.  Listing it explicitly in the documentation was a historical mistake and has been fixed since, see: https://github.com/pypa/setuptools/commit/f7d30a9529378cf69054b5176249e5457aaf640a

I'm just following what is listed here: https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/#fallback-behaviour

And as recently as this week by another one of the pypa team: https://github.com/pypa/pip/issues/8559#issuecomment-1310317396

> I'm just following what is listed here: https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/#fallback-behaviour

Well, I don't think it's needed for the "fallback" code path either but even if it were, you are not supposed to use that backend. It's just "what happens when there's no pyproject.toml".

> And as recently as this week by another one of the pypa team: [pypa/pip#8559 (comment)](https://github.com/pypa/pip/issues/8559#issuecomment-1310317396)

Unfortunately, it will probably take years before people realize they're copying a mistake from the past. That's why I'm always linking to the official documentation.

>Unfortunately, it will probably take years before people realize they're copying a mistake from the past. That's why I'm always linking to the official documentation.

I agree, and would add my own opinion that pypa is an absolute joke.

I'm going to leave it in. Call it cargo-culting, call it senseless, but I just don't care to spend the effort to try and learn all the intricacies of these various new tools/their interactions/and what combination of what versions does what (wheel, pyproject, pip, setuptools). They'll probably change everything again in a year or two.

Well, the whole point is that they are considering actually stopping to use `wheel` (though it was a while back, back when wheel received zero maintenance) in favor of a built-in wheel generator in setuptools.