akumoth/peewee-issues huggingface dataset:
Issue: TimestampField uses an undocumented power of 10
Author: Sekenre
State: closed
Created: 2019-09-05 09:33:14

If I create a TimestampField(resolution=1) it will unexpectedly multiply the timestamps by 10.

This is different from the default resolution of 1 second if the resolution is set to None.

Resolution as a power (`10 ** x`) where x is in `range(7)` is undocumented and maybe a bit confusing. To resolve this in my own code I had to set `resolution=0` which will make it harder for someone to read my intent.

Originally this was expressed as 1, 10, 100, ... 1000000. I added the option to provide it as the exponent instead, but this broke the default of 1 (turning it into 10).

Fixed so that 0 and 1 both resolve to seconds resolution. To get 10th of a second, one needs to supply the resolution=10, but I don't think this is likely to be an issue. People tend to either use seconds, milliseconds or microseconds.
Issue: DecimalField does not work correctly
Author: ccodde
State: closed
Created: 2019-09-04 22:40:19

When choosing a large type accuracy, the accuracy is not saved and the data is stored incorrectly.

```python
from peewee import *
from decimal import Decimal

class Test(Model):
    value = DecimalField(max_digits=26, decimal_places=18)

db = SqliteDatabase(':memory:')
db.bind([Test])

Test.create_table()
Test.create(value=Decimal('12345678.123456789123456789'))
row = Test.get()
print(row.value)
# 12345678.12345679
```


Save the number 12345678.123456789123456789
When extracting data, we get 12345678.12345679

Sqlite only supports a couple different data-types: https://sqlite.org/datatype3.html

Sqlite interprets DECIMAL fields as REAL, which is a double-precision / float64. So that is why you are losing precision. Sqlite is optimizing the storage by treating the value as a numeric quantity, but it can only implement the value as an int64 or a float64.

If you don't plan on doing any math on it ever, you could use a text column for the storage:

```python
class SqliteDecimalField(DecimalField):
    field_type = 'TEXT'
```

The above will store the value precisely, but it is stored as a string so as soon as you try to do math on it you'll run into the same types of issues as before.

Basic database literacy is a good thing. I'd suggest reading the sqlite docs next time.
Issue: Chalice Python framework with Peewee
Author: jlpl1999
State: closed
Created: 2019-09-04 03:56:05

How do i do the before/after connection with mysql/aws RDS along with chalice serverless framework  using PeeWee as ORM? thanks

This is for tracking **issues**, not "how-do-i" questions. Please take it to stack overflow.

http://docs.peewee-orm.com/en/latest/peewee/contributing.html#questions
Issue: can't support 5.5.*-mariadb
Author: City-Zero
State: closed
Created: 2019-09-03 09:33:45

https://github.com/coleifer/peewee/blob/cc02d32d05acfdfa2b4d01d99f646b56b6a3c754/peewee.py#L3813

That's an ancient version of mariadb, first of all. Second of all, it doesn't matter because that version is so old it doesn't support any of the features that the version-check is designed to flip.
Issue: DecimalField does not work correctly
Author: twixmix
State: closed
Created: 2019-09-03 01:53:37

When choosing a large type accuracy, the accuracy is not saved and the data is stored incorrectly.


```python
from peewee import *
from decimal import Decimal

class Test(Model):
    value = DecimalField(max_digits=26, decimal_places=18)

db = SqliteDatabase(':memory:')
db.bind([Test])

Test.create_table()
Test.create(value=Decimal('12345678.123456789123456789'))
row = Test.get()
print(row.value)
```

Save the number 12345678.123456789123456789
When extracting data, we get 12345678.12345679
Issue: The Document's issue - "Model.first()"  exists, but the method had disappeared yet
Author: linusic
State: closed
Created: 2019-08-30 09:26:30

thanks for "peewee".
I saw a small  issue   when I read the  document .

Document version:  latest

and look follow pic.
![image](https://user-images.githubusercontent.com/30204682/64009446-7146e200-cb4a-11e9-8802-f173503c7117.png)


The Method is still there:

    def first(self, database, n=1):

https://github.com/coleifer/peewee/blob/b51b230a45e71464d90607ccfdd40ccff4d2d3f8/peewee.py#L2049

thx for u response
I see,  I use it incorrect ,sry!

I think it use  likely   the "get()" method. 
so  i use it by mistake -> 
    # Owner.first()   # error use  

but it should use correctly ->
    Owner.select().where(Owner.name=='Alice').first()   # right use

Beat me to it @oz123 thanks for helping out a newcomer.
Issue: Column casting doesn't work in 3.10.0
Author: cubez
State: closed
Created: 2019-08-29 14:37:56

Up until 3.9.6 I could do this:

```python
TableName.select().where(TableName.some_int_field.cast("CHAR").contains("abc"))
```
The result would just be empty. But now in 3.10.0 I get this error:

```sh
ValueError: invalid literal for int() with base 10: '%abc%'
```
Looking through the changelog I can't really find anything that would cause this. So I don't know if this is a bug or a feature. :)

I can see both sides, but I do think that I will make the logic for adapting values more permissive. What changed is that an expression now looks a little bit harder to see if the right-hand-side can be converted using the conversion implied by the left-hand field. For a cast(), however, this should be more permissive.

Should be working again: 92713817185aa7decf29234fe704ba2182c10196

Actually, I changed my mind about how to fix this. Instead, I will look at the `_coerce` internal flag and use that to determine whether to apply the conversion.

42e2b429c226590bac20770a8a10a4b4a9b0c897

If it works, it works. 👍 Thank you for the fast fixing.
Issue: conda-forge version of 3.10.0
Author: sterlingbaldwin
State: closed
Created: 2019-08-26 15:25:29

The latest version published to conda-forge is 3.9.2. It would be nice to have 3.10.0. 

get out of here with this crap, i don't have anything to do with whatever that is.

Ah, I just assumed you were the maintainer for the conda-forge feedstock, but it looks like its someone else. Peewee has +100k installs through conda-forge https://github.com/conda-forge/peewee-feedstock

Holy smokes! Never heard of it before.
Issue: Question - Is there a way not to create a base model table in the database?
Author: dsmurrell
State: closed
Created: 2019-08-25 20:32:35

I did scan the docs briefly, but nothing obvious popped out at me. I want to have the following configuration but not have the base table be created in the database as it's never used by me. Is there a way to do this?

```
class BaseModel(Model):
    class Meta:
        database = db

class User(BaseModel):
    username = CharField(unique=True)
```

The base model *is not* created... you have to explicitly tell peewee which tables to create. It doesn't matter the inheritance chain -- each model class is just a single table.

Thanks! 

I've been using peeweedbevolve for too long which creates all by default. Just found the answer here for my use case.

https://github.com/keredson/peewee-db-evolve/issues/24#issuecomment-360875831
Issue: Support for Triggers and LISTEN/Notify
Author: iguanaus
State: closed
Created: 2019-08-23 20:55:22

Great work on the library, it has been very flexible and quick to work with.

We are needing to notify a service whenever a table is updated. Our backend database is Postgres. Listen/Notify seems to optimally fit out needs. Ideally we need a way to trigger or notify on creation/updates, and then subscribe with polling to get responses.

## Pub implementation

On the pub side, there are two ways to implement this:

- A trigger on the table (similar to that described [here](https://stackoverflow.com/questions/5412474/using-pg-notify-in-postgresql-trigger-function))
- Bundling each insert/update in a transaction, and putting a notify at the end of it.

From a simplicity perspective, I would rather put a trigger in the table for the pub, then have a polling feature setup for subscribe. Does PeeWee or any integrated library via playhouse support triggers and adding them? I found a hack [implementation](https://stackoverflow.com/questions/34142550/sqlite-triggers-datetime-defaults-in-sql-ddl-using-peewee-in-python), is this what you would recommend if we wanted to add a trigger to the schema? This seems like a sensible addition at some point considering the power of pub/sub in postgres.

The other down-and-dirty implementation of this is to override the insert/update methods in the model and just turn them into transactions with a NOTIFY call at the end (we already do this for some things, such as insert_or_update where we query if the record exist and if not create, or if so update. Note that we do not use autoincrementing primary keys as we regularly create more than 2 billion records; we use uuid, so we lose a bit of your methods).

## Sub implementation

A sample implementation of this can be seen quickly on the psycopg website [here](http://initd.org/psycopg/docs/advanced.html#asynchronous-notifications). Is such a thing possible with PeeWee? How would you recommend building this?

Thank you a million, happy to do a MR to support the above if useful.



There are huge differences between sqlite/mysql/postgres for writing triggers. If you need a trigger, just create it after your call to `db.create_tables(...)`:

```python
db.create_tables([MyModel, ...])
db.execute_sql('create trigger ...')
```

For LISTEN/NOTIFY, again, I'm not sure how Peewee would improve on this much as it's pretty straightforward. Just use `db.execute_sql('listen channel_name')` to subscribe, and then for notify:

```python
payload = {'now': time.time(), 'data': 'something'}
db.execute_sql('notify channel_name, %s', (json.dumps(payload),))
```

The psycopg2 docs contain an example of polling for updates, presumably you'd do this in a separate thread or use a coroutine library.
Issue: db_value and model fields
Author: bronte2k7
State: closed
Created: 2019-08-20 18:20:26

Hi guys

```python
class CustomField(CharField):
	def db_value(self, value):
		# can I access the note field?
		return value

class User(Model):
	note = TextField(null=True)
	extend = CustomField()
```

Is it possible to access the value in the note field, from db_value?

No
Issue: Improve equality check
Author: clintonb
State: closed
Created: 2019-08-18 20:40:00

The current implementation of `__eq__` on `Model` just checks if the primary keys are equal.
https://github.com/coleifer/peewee/blob/f6d693366eccb8e27b0b331246358f9c7d4e54b7/peewee.py#L6364-L6368

It would be preferable to if this method compared the value of all fields.

Test case:
```python
from models import Test
t1 = Test(id=1, foo='bar')
t2 = Test(id=1, foo='apple')
assert t1 != t2
```

Context: When writing tests, I prefer to compare two instances rather than iterate over all of the fields.

Here is a proposed implementation:

```python
def __eq__(self, other: Any) -> bool:
    if other.__class__ != self.__class__:
        return False

    for field in self._meta.sorted_fields:
        if getattr(self, field.name) != getattr(other, field.name):
            return False

    return True
```

I can't imagine a situation in practice where such a comparison would be valuable. It suggests to me that there's a problem elsewhere in the implementation of your code. Models are not a representation of the state of the DB at all times. It's just simple active record. As such, comparing IDs seems reasonable but it's best to never need to compare model instances at all because of this.
Issue: Set last_insert_id(`id`) on Insert ... On Duplicate Key Update in MySQL
Author: malaney
State: closed
Created: 2019-08-16 09:14:31

I'm using peewee to generate an "INSERT ... ON DUPLICATE KEY UPDATE" query in MySQL using the .insert().on_conflict() methods for a table with an auto_increment ID.  Whether the query performs an INSERT or an UPDATE I'd like the query to return the ID of the most recently created row or the ID of the most recently updated row.  In MySQL this can be achieved by building a query that looks like the following:

`INSERT ... ON DUPLICATE KEY UPDATE `id`=last_insert_id(`id`)`

With this query, the very next call to last_insert_id will always return the right value whether the operation performs an insert or update.  Is it possible to get peewee to render such a query using the Model.insert().on_conflict() API??

I just answered my own question!!

> .on_conflict(
                            preserve=[Model.first_name, Model.last_name],
                            update={Model.id: fn.last_insert_id(Model.id)}
                        )
Issue: pwiz creates all fields as FloatField regardless of precision
Author: albireox
State: closed
Created: 2019-08-09 18:05:56

It seems when `pwiz` creates a schema, it assigns `FloatField` to all the floating point types, regardless of their precision. Could it be possible to have it create the appropriate type of field?

Alternatively, is there a way to access the Postgresql underlying type for a field without having to connect to the DB?

Pushed a fix -- oid 701 appears to be double-precision, so it's up-to-date now. If your schema already exists, however, it doesn't matter because Python uses a double internally anyways. Lastly, if it's an issue, you are free to modify the fields yourself.

> Alternatively, is there a way to access the Postgresql underlying type for a field without having to connect to the DB?

I don't know what this means.

Thanks, that works. My use for this case is that I have a tool that uses the introspection utils to generate PDF with the DB tables and columns, and it was assigning FLOAT to all the columns regardless of precision.

> > Alternatively, is there a way to access the Postgresql underlying type for a field without having to connect to the DB?
> 
> I don't know what this means.

I meant if it's possible to get the PSQL type for a field (e.g., 'BIGINT', 'DOUBLE', 'REAL', 'VARCHAR', etc.)

> I meant if it's possible to get the PSQL type for a field

Check the code in reflection, it has all the info you need. You can map the oid to the typname in the pg_type table.
Issue: Enumerating over query result
Author: karambaq
State: closed
Created: 2019-08-08 16:38:16

I want to get next element after the predicate is true

I've tried to use 
```
    for i in enumerate(gs):
        if predicate(i[1]):
            print(i)
```
Output: 
```
(0, <Fighter: 202157127>)
(0, <Fighter: 202157569>)
(0, <Fighter: 202158125>)
(0, <Fighter: 202159063>)
(0, <Fighter: 202160131>)
(0, <Fighter: 202160498>)
(0, <Fighter: 202160800>)
```

I need to catch that `i + 1` element, how can I do that?

http://docs.peewee-orm.com/en/latest/peewee/contributing.html

Post this on stackoverflow or ask in IRC. This forum is for issues.
Issue: Column with name 'insert' replaces Model class method 'insert'
Author: wspek
State: closed
Created: 2019-08-01 15:47:38

Version: 3.9.6

I have discovered that when I create a Model which has a column literally named **insert**, the peewee code will throw an exception when trying to insert data through the `Model.insert` class method in [line 6311](https://github.com/coleifer/peewee/blob/ef27f0209a7942e28ba976264ceaf85891277bb1/peewee.py#L6311):

`pk = self.insert(**field_dict).execute()`

To reproduce simply create a model which has a column named **insert**. In my case, after adding a Model with the column named **insert**, things look like this:

![image](https://user-images.githubusercontent.com/23557813/62306771-d37fca80-b458-11e9-9eb7-f89118144d1e.png)

Note the type `None` of the **insert** class method, which is due to the fact that now a column exists in the model with value `None`. After the actual field data is added, in my case, the **insert** field has value `False`. Because the code then tries to call `execute()` on this boolean value, ultimately my code fails with an exception:

`ERROR 'bool' object is not callable`

My Model also has columns **delete** and **create**, which may cause problems by overwriting class methods with the same names. That is because I am storing a table which contains database permissions for an unrelated software solution.

Thanks.


This is why we have stuff like "column_name":

```python
class MyModel(Model):
    insert_ = TextField(column_name='insert')
```

@coleifer 

I have made sure all my columns are explicitly named now by using your suggestion. I hope this GIF demonstrates well that even though the **insert** column has an explicit `column_name` set, the `self.insert` class method is still assuming the value of the field.

![Peek 2019-08-01 13-32](https://user-images.githubusercontent.com/23557813/62311096-6886c180-b461-11e9-897c-95aade3c2bf5.gif)


Sorry, I think I get your point. I have to pay attention to the underscore you wrote down.
Issue: Bulk update of json field not producing executable SQL in Postgres
Author: saileshr
State: closed
Created: 2019-07-31 18:21:48

```
class Merchant(BaseModel):
    name = TextField(primary_key=True)

class Metrics(BaseModel):
    merchant = ForeignKeyField(backref='metrics', field='name', model=Merchant, primary_key=True)
    db_tx_count = JSONField(null=True, default={})
```

I update a bunch of metrics records and store them in metrics_updated.

I then call:
```
Metrics.bulk_update(metrics_updated, fields=['db_tx_count'])
```

That produces the following SQL:

```
'UPDATE "metrics" SET "db_tx_count" = CASE "metrics"."merchant_id" WHEN %s THEN %s WHEN %s THEN %s END WHERE ("metrics"."merchant_id" IN (%s, %s))', ['Hobby Lobby', <psycopg2._json.Json object at 0x7f06c30d5cc0>, 'Caserta Pizzeria Bakr', <psycopg2._json.Json object at 0x7f06c30d5ef0>, 'Hobby Lobby', 'Caserta Pizzeria Bakr'])
```
which triggers the following postgres error:
```
peewee.ProgrammingError: column "db_tx_count" is of type json but expression is of type text
LINE 1: UPDATE "yodlee"."metrics" SET "db_tx_count" = CASE "metrics"...
                                                      ^
HINT:  You will need to rewrite or cast the expression.
```

Postgres 11.2.  Python 3.6.7  Peewee 3.9.6
Please let me know if you need any other info


I'm not in love with the way I've patched, this, but I have replicated the issue and believe it is fixed:

ef27f0209a7942e28ba976264ceaf85891277bb1

I'd like to get a better handle on exactly when and where to apply such casts, but until I dive in deeper I suppose this will work.

thanks for the quick response and the fix.

Doesnt appear to be fixed.. but the error now is different:
```
peewee.ProgrammingError: syntax error at or near "END"
LINE 1: ... SET "db_tx_count" = CASE "metrics"."merchant_id" END, "db_a...
```

Perhaps you're passing an empty list? Not sure, however there is a test that is passing if you bother seeing the commit.

@coleifer same thing happens with UUIDField.
I inherited the UUIDField and applied the same fix, and seems to work well
Issue: MySQL does not support the specification of where clauses...
Author: stormeyes
State: closed
Created: 2019-07-30 12:11:36

Hi, I read the issue https://github.com/coleifer/peewee/issues/1307 and know I can use upsert-like method `on_conflict`, however when I try I got error: `MySQL does not support the specification of where clauses or conflict targets for conflict resolution.` , anyone knows why?

```
res = (Emp
       .insert(first='foo', empno='125')
       .on_conflict(
            conflict_target=(Emp.empno,),
            preserve=(Emp.first, Emp.last),
            update={Emp.empno: '125.1'})
        .execute())
```
Issue: Trouble Splitting Up Code
Author: dkryptr
State: closed
Created: 2019-07-29 16:05:56

The issue I'm running into is pretty specific to my use case and I'm not sure how many it would affect. I'm creating AWS Lambda functions that insert/query from the database. I have my code separated into multiple files so that different lambda functions can re-use code. Here's a very simplified example.

db.py
```python
import os
from peewee import PeeweeException, Model, MySQLDatabase

hostname = os.environ["DB_HOST"]
username = os.environ["DB_USER"]
password = os.environ["DB_PASSWORD"]
database = os.environ["DB_DATABASE"]

try:
    db = MySQLDatabase(
        database=database,
        user=username,
        password=password,
        host=hostname,
        charset="utf8",
        use_unicode=True,
    )
except PeeweeException as e:
    raise


class BaseModel(Model):
    class Meta:
        database = db
```

main.py
```python
import json
import os

import service
from db import db


def handler(event, context):
    try:
        db.connect()
        response = service.run(db, event)
        db.close()
    except Exception as e:
        return {
            "statusCode": 500,
            "body": json.dumps(
                {
                    "code": "InternalFailure",
                    "message": "Internal server error",
                }
            ),
        }

    return response
```

I'm excluding a few files(`model.py`, `service.py`) but `service.run(db, event)` imports model and inserts/queries the database using the models it imports.

The problem occurs when I try to write unit tests for main.py. Since main.py imports db, it immediately tries to read environment variables. So when my unit tests import main to test it, immediately my unit tests fail because those environment variables don't exist. I could easily mock out `os` but I don't think main.py should be calling db.py immediately anyways. So I've tried wrapping some code in db.py in a get_connection() function:

```python
def get_connection():
    hostname = os.environ["DB_HOST"]
    username = os.environ["DB_USER"]
    password = os.environ["DB_PASSWORD"]
    database = os.environ["DB_DATABASE"]

    try:
        db = MySQLDatabase(
            database=database,
            user=username,
            password=password,
            host=hostname,
            charset="utf8",
            use_unicode=True,
        )
    except PeeweeException as e:
        raise

class BaseModel(Model):
    class Meta:
        database = db
```

But the problem with this is that db is undefined. Anyways, the long story short: I've tried a lot of things and I'm unable to figure it out and was wondering if you had any tips for me?

> But the problem with this is that db is undefined.

Because you're not returning anything...

This is not peewee-specific but a general question about Python module organization. When you have dependencies like this, the typical solution is to introduce a third module that composes the two (db and main).