akumoth/peewee-issues huggingface dataset:
Issue: [Feature request] pg8000 support
Author: vaygr
State: closed
Created: 2017-05-19 01:32:04

It'd be nice to have one pure-Python PostgreSQL driver supported to minimize amount of dependencies installed on the system.

I think [pg8000](https://github.com/mfenniak/pg8000) is a great candidate.

Is it something not considered as a high priority at the moment?

I've also seen people trying to implement this, e.g. [here](https://www.quora.com/Is-it-possible-to-swap-in-a-pg8000-backend-for-psycopg2-when-using-Peewee).

You can implement it by doing something like the above code snippet.

```python
class PostgresqlDatabasePG8000(PostgresqlDatabase):
    ...
```

Yeah, but is there any chance it could get official support in peewee in the future?
Issue: TimestampField reads `0` as `null`
Author: avivey
State: closed
Created: 2017-05-18 21:00:36

I guess this is the relevant line https://github.com/coleifer/peewee/blob/master/peewee.py#L1346

At least for mysql, if we have a TimestampField that's defined like this:
```
not_before = TimestampField(default=0, utc=True, null=False)
```
the DB type is `int(11) NOT NULL`.

If the DB gets a value of `0` (For any reason, like "I think it's a valid value" or "The data is already there"), trying to update the value fails:
```
x = Task.get(...)
x.status = 'good'
x.save()

      pymysql.err.IntegrityError: (1048, "Column 'not_before' cannot be null")
```

I'm not sure about the reasoning behind this, as the code looks intentional.

This is confusing to me -- why use `0` instead of `null`? A zero timestamp corresponds to midnight Jan 1st, 1970.

Not sure what you're asking. `0` is not the same as `null`, but the `TimestampField` thinks they are. I think it's a bug.

I guess what I'm asking is why you're using `0` instead of `NULL`, when `0` for a timestamp represents midnight jan 1st, 1970?

Because I want a non-null value there. `Null` means "no value", `0` means "midnight, Jan 1st, 1970".

Why should the ORM read `0` as `None`? 

That's fine -- as long as you were aware of the distinction.

Added support for specifying the zero value for the `TimestampField`. You can now write:

```python
timestamp = TimestampField(zero_value=datetime.fromtimestamp(0))
```

Thanks!
Issue: Peewee >= 2.4.3 is required. Found version ...
Author: thebalthasar
State: closed
Created: 2017-05-15 10:00:06



The instruction which is reported while doing the version check is misleading. This is the runtime error and its message:
RuntimeError: Peewee >= 2.4.3 is required. Found version 2.10.1. Please update by running pip install --update peewee

Should be instead:
**pip install peewee --upgrade**

The error comes from comparing versions in line 58 of sqlite_web.py

>>>version_old='2.4.3'
>>>version_new='2.10.1'
>>>version_new>version_old 
**False**
A better comparison would be:
>>> from distutils.version import StrictVersion
>>>StrictVersion(version_new) > StrictVersion(version_old)
**True**

Thank you for your work!
:-)


Yes, this is fixed in sqlite-web. See the issue there: https://github.com/coleifer/sqlite-web/issues/30

sqlite-web 0.1.8 was released as well, fixing the issue if you try again.
Issue: [playhouse.dataset] Cannot process column name contain upper case char
Author: renweibo
State: closed
Created: 2017-05-14 01:16:51

Here is the test case.
```python
    def test_case_2(self):
        db = DataSet('sqlite:///:memory:')
        books = db["books"]
        books.insert(bookId="ISBN1234567")
```
It should work but report following error:

```python
Traceback (most recent call last):
... ...
  File "/Users/renweibo/PycharmProjects/DailyNovel/common/test_driver.py", line 17, in test_case_2
    books.insert(bookId="ISBN1234567")
  File "/Users/renweibo/.virtualenvs/flask1/lib/python3.6/site-packages/playhouse/dataset.py", line 219, in insert
    return self.model_class.insert(**data).execute()
  File "/Users/renweibo/.virtualenvs/flask1/lib/python3.6/site-packages/peewee.py", line 4893, in insert
    fdict.update([(cls._meta.fields[f], insert[f]) for f in insert])
  File "/Users/renweibo/.virtualenvs/flask1/lib/python3.6/site-packages/peewee.py", line 4893, in <listcomp>
    fdict.update([(cls._meta.fields[f], insert[f]) for f in insert])
KeyError: 'bookId'
```


Version:
- Python: 3.6.1
- peewee: 2.10.1 

d68ae11501face8f4eb6adfde82d8eda79728dc7
Issue: [playhouse.dataset.DataSet] Cannot process column name contain "_"
Author: renweibo
State: closed
Created: 2017-05-14 01:12:41

Here is the test case.
```python
    def test_case_1(self):
        db = DataSet('sqlite:///:memory:')
        books = db["books"]
        books.insert(book_id="ISBN1234567")
```
It should work but report following error:

```python
Traceback (most recent call last):
... ...
  File "/Users/renweibo/PycharmProjects/DailyNovel/common/test_driver.py", line 12, in test_case_1
    books.insert(book_id="ISBN1234567")
  File "/Users/renweibo/.virtualenvs/flask1/lib/python3.6/site-packages/playhouse/dataset.py", line 219, in insert
    return self.model_class.insert(**data).execute()
  File "/Users/renweibo/.virtualenvs/flask1/lib/python3.6/site-packages/peewee.py", line 4893, in insert
    fdict.update([(cls._meta.fields[f], insert[f]) for f in insert])
  File "/Users/renweibo/.virtualenvs/flask1/lib/python3.6/site-packages/peewee.py", line 4893, in <listcomp>
    fdict.update([(cls._meta.fields[f], insert[f]) for f in insert])
KeyError: 'book_id'
```

Version:
- Python: 3.6.1
- peewee: 2.10.1 

The problem is the "_id" suffix. DataSet relies on the introspection tools in the `playhouse.reflection` module. These tools are used to read the database and generate peewee Model definitions. There's a convention in peewee, borrowed from Django, to append "_id" to foreign key fields. So if I have a Tweet and User model, and the Tweet.author is a foreign key to a User, the column will actually be called Tweet.author_id (though the field name is "author"). This comes into play when introspecting, which does the inverse -- if it sees a column named "xxx_id", it will strip the "_id" from the field name (preserving it for the column).

I'll look into how to fix this, at any rate.

d68ae11501face8f4eb6adfde82d8eda79728dc7

Thanks for the quick fix and support.  

For sure, thanks for the nice reports.
Issue: Update method dont work
Author: pandbsoftware
State: closed
Created: 2017-05-13 06:51:26

```python
from peewee import *
from playhouse.sqlite_ext import SqliteExtDatabase
import datetime

db = SqliteExtDatabase('lis22.db')

class BaseModel(Model):
    class Meta:
        database = db

class Lista(BaseModel):
    cuenta = CharField(unique=True)
    tiempo = CharField()

try:
    db.connect()
    db.create_tables([Lista])
except:
    pass

lista = Lista.create(cuenta="test", tiempo="a")
a = Lista.update(cuenta="test_changed").where(Lista.cuenta == "test")
a.execute()
Lista.get(Lista.cuenta == "test").cuenta

#output:  u'test'
```

Try property indenting and surrounding your code in code blocks.

Please take the extra minute to ensure your code is properly formatted. I've done it for you this time as a courtesy.

There is something wrong in your code or in the state of your database, because I get an error (as expected):

```python
Traceback (most recent call last):
  File "x.py", line 25, in <module>
    obj = Lista.get(Lista.cuenta == "test")
  File "/home/charles/pypath/peewee.py", line 4926, in get
    return sq.get()
  File "/home/charles/pypath/peewee.py", line 3185, in get
    % self.sql())
__main__.ListaDoesNotExist: Instance matching query does not exist:
SQL: SELECT "t1"."id", "t1"."cuenta", "t1"."tiempo" FROM "lista" AS t1 WHERE ("t1"."cuenta" = ?)
```
Issue: Tests in separate modules
Author: ardeaf
State: closed
Created: 2017-05-12 21:14:21

I followed the quickstart guide and wasn't as thorough as I should have been with the docs, eventually locating the "extra" section which solved my problem. However, I think adding to the docs a best practices note in the quickstart on how to do tests with peewee would be beneficial.

My tests were in separate module, so I didn't know I was using a different db connection in my test modules when I made the db variable at the beginning of my test modules. I was trying to use transactions:

main/models.py
```
from peewee import *

db = SqliteDatabase('/home/ardeaf/my_app.db')

class BaseModel(Model):
    class Meta:
        database = db

class User(BaseModel):
    username = CharField(unique=True)
```

tests/test_models.py
```
from main.models import User

db = SqliteDatabase('/home/ardeaf/my_app.db')

test_save_user():
    db.atomic() as txn:
        username.Create("ardeaf")
        assert User.get(User.username == "ardeaf").username == "ardeaf"
        txn.rollback
````

So that wasn't working -- admittedly, I'm an idiot.  Tests passed, but database wasn't being rolled back. I eventually figured out the whole connection thing and ended up making my database dynamically defined at runtime.

However, it might be great to add a how-to on this in the quickstart docs in case someone was writing tests. I believe this would also be the case if you end up using the models in different modules, right? Perhaps a quick note explaining how to do that would help.


This is not Peewee, this is Python. The database attribute of the Model's `Meta` class holds a reference to the `db` object a module scope... This is resolved at module-load time. So no magic.

@ardeaf That's an easy mistake to make. If you want to be absolutely sure to use the right connection, I think you could do:

    with BaseModel.Meta.database.atomic():

But that's super ugly, yeah...
Issue: FTSModels for sqlite - weights in search() and search_bm25() don't work.
Author: eugeniominissale
State: closed
Created: 2017-05-11 09:38:27

I'm having an issue using the sqlite_ext FTS extension.
That's the example of the documentation:

```from playhouse.signals import Model
from playhouse.sqlite_ext import SqliteExtDatabase, FTSModel, SearchField
from peewee import TextField

db = SqliteExtDatabase(':memory:', threadlocals=True)


class Document(Model):
    title = TextField(null=False, unique=True)
    content = TextField(null=False)

    class Meta:
        database = db


class DocumentIndex(FTSModel):
    title = SearchField()
    content = SearchField()

    class Meta:
        database = db
        extension_options = {'tokenize': 'porter'}


def store_document(document):
    DocumentIndex.insert({
        DocumentIndex.docid: document.id,
        DocumentIndex.title: document.title,
        DocumentIndex.content: document.content}).execute()


def search(phrase):
    docs = DocumentIndex.search(
        phrase,
        weights={'title': 2.0, 'content': 1.0},
        with_score=True,
        score_alias='search_score')

    for result in docs:
        print(result.title, result.search_score)


def search_bm25(phrase):
    best_bm25 = DocumentIndex.search_bm25(phrase,
                                          weights=[2.0, 1.0],
                                          with_score=True).tuples()

    for result in best_bm25:
        print(result)


Document.create_table(fail_silently=True)
DocumentIndex.create_table(fail_silently=True)
DocumentIndex.rebuild()
DocumentIndex.optimize()

if __name__ == '__main__':
    doc1 = Document.create(
        title="documento",
        content="mario")

    doc2 = Document.create(
        title="mario",
        content="descrizione")

    store_document(doc1)
    store_document(doc2)

    search('mario')

    search_bm25('mario')
```

The ouput given is not what I expect. 

```
documento -0.0
mario -0.0
('documento', 'mario', -0.0)
('mario', 'descrizione', -0.0)
```

Seems like when I put some weights inside the functions the calculated score is automatically set to -0.0.
Am I missing something?

I found a bug in the Python implementation of the default rank() function...the Cython version worked fine, but the Python version wasn't respecting the weights parameter due to some careless variable naming on my part. When I run it locally, I now get the following output:

```python
(u'mario', -2.0)
(u'documento', -1.0)
(u'documento', u'mario', -0.0)
(u'mario', u'descrizione', -0.0)
```

This doesn't address your question about BM25, but the BM25 scoring function is actually unit-tested and I know from using it on my own projects that it does work. To ensure this, I've added an additional unit-test that specifically tests the weight parameter. You can view the code here: cfd5aee9f85352a7a7b0054467eaa0d6ad5b87d9

I think that perhaps the `0.` results are simply a result of the sample size being so constrained.
Issue: db.create_table() and db.create_tables([,]) have different behavior？
Author: piperck
State: closed
Created: 2017-05-10 03:00:13

```python
class Account(BaseModel):

    class Meta:
        db_table = 'account'
        indexes = (
            (('app_id', 'user_id'), True),
        )

    app_id = IntegerField()
    user_id = IntegerField()
    amount = DecimalField(15, 2)
    create_time = DateTimeField(default=datetime.datetime.now)
    update_time = DateTimeField(default=datetime.datetime.now)
```
I try to create my tables.And I found if I use `create_table(Account)` can not make `indexes=(xx)` coming into effect.
I use `show create table account` check my mysql_tables. It shows
```sql
| account | CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `app_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `amount` decimal(15,2) NOT NULL,
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
```

But I use `create_tables([Account,])`, It works！
```sql
| account | CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `app_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `amount` decimal(15,2) NOT NULL,
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `account_app_id_user_id` (`app_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
```

Is this a problem or expectable?

These methods are very confusingly named -- apologies for that.

History:

Originally, you would use `Model.create_table()` which would create the table along with the indexes and other constraints. The `Database.create_table()` method was used behind-the-scenes and it **only** creates the table -- not indexes.

Then I realized there is a common situation where you need to create many tables, and create them in the right order so that the dependency graph is created correctly. To accomplish this, I added the `Database.create_tables()` method (table**s**). It accepts a list of models, does a topological sort to obtain a total ordering, then calls `Model.create_table()` on each table in succession, which ensures the indexes are created as well.

So, long story short, `Database.create_table()` should not be used unless you have a very specific reason for using it.

You should use `Model.create_table()` to create a single model's table & indexes, and `Database.create_tables()` when you want to create tables & indexes for more than one model.

Ok! Thanks for your long story and long story short !!
 It entirely solve my problem!🤣😇
Issue: Pwiz requires workaround for columns beginning with numbers
Author: coleifer
State: closed
Created: 2017-05-10 02:11:30

I'm trying out peewee on one of our existing development mysql databases. I've run pwiz and generated a database model. I've noticed a python/db problem with class variables starting with numbers. Does peewee have a solution for dealing with this issue? I couldn't find anything addressing this problem in the documentation.

```python
class AshFinderGryM2F2G3F3G4Rat1D3HMin13ETests(BaseModel):
    4m5_min = FloatField()
    alarm = IntegerField()
    alarm1 = IntegerField()
    alarm2 = IntegerField()
    alarm3 = IntegerField()
    ...

class AshFinderRayRealdaytimeTests(BaseModel):
    10_perc_4m5 = FloatField(null=True)
    10_perc_ash_group_num_pix = IntegerField(null=True)
    90_perc_4m5 = FloatField(null=True)
    90_perc_ash_group_num_pix = IntegerField(null=True)
    alarm = IntegerField()
    alarm_4m5 = IntegerField()
    ...
```

--- received in contact form sent through my site.
Issue: Child table referencing a mother table
Author: martinjoubert
State: closed
Created: 2017-05-09 06:30:09

Hi!

I can't figure out how to get a table referencing itself. My goal is to get a child table referencing a mother table with same types.

This code seems to be not working:

DeferredMyTable = DeferredRelation()

class MyTable(BaseModel):
    name = TextField()
    image = TextField()
    root_my_table_id = ForeignKeyField(MyTable, null=True)

    class Meta:
        db_table = 'MyTable'

DeferredMyTable.set_model(MyTable)

Am I wrong when I design my table like that or is it an issue?

Regards,

Gaël

That works perfectly in the end. My mistake was elsewhere.

Have a nice day!
Issue: Bad Magic Number
Author: ahopkins
State: closed
Created: 2017-05-09 06:02:10

I created a new virtualenv to test out peewee. I was simply trying to follow the [Quick Start](http://docs.peewee-orm.com/en/latest/peewee/quickstart.html).

    mkvirtualenv peweetest -p python3
    python
    >>> import peewee
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: bad magic number in 'peewee': b'\x03\xf3\r\n'

I have python 3.5 running (I also tried with 2.7 and 3.6), and peewee is on v 2.10.0.

Anyone else see this? Ideas?


It seems maybe someone should look into the pypi package. I installed via git, and that seemed fine.

Thanks for bringing this to my attention. Pushed a new minor release and verified it is working correctly for me using 2.7 and 3.6 + PyPI.
Issue: Fix AESEncryptField for python3
Author: Moutix
State: closed
Created: 2017-05-05 21:07:00

In python3 you can't create a bytes string with bytes(' ') because you need to specify an encoding.

```
2017-05-05 22:34:43,025 ERROR      File "/home/moutix/.virtualenvs/stepmania-server/lib/python3.5/site-packages/playhouse/fields.py", line 320, in get_cipher
    key = key + bytes(' ') * (32 - len(key))

2017-05-05 22:34:43,025 ERROR    Uncaught exception: string argument without an encoding
```

Related to: https://github.com/coleifer/peewee/issues/1262

The tests for the playhouse extra_fields are not run automatically. Is there a particular reason?

Will all those byte literals be compatible with Python 2.6?

See comments on #1262 and #1264
Issue: Encryption with python3 throws TypeError
Author: cym13
State: closed
Created: 2017-05-05 21:02:41

Hi,

the AESEncryptedField in playhouse/fields.py is broken in python3.

Line 320 we see:

            key = key + bytes(' ') * (32 - len(key))

but bytes(' ') isn't valid python3 and will throw a TypeError.

This field has been removed in the latest release, 2.10.0. For details and explanation of why, see https://github.com/coleifer/peewee/releases/tag/2.10.0
Issue: Aiopeewee
Author: kszucs
State: closed
Created: 2017-05-05 09:12:07

I've created an asyncio wrapper for peewee, adapted ideas from torpeewee.
It isn't feature complete yet, but I'm working on it.

@coleifer Could You give me a general feedback?

I need to duplicate a lot of code (due to asyncio nature), but I'm wondering how could I reduce it. 
Could You give some advice?



@kszucs Have you seen https://github.com/05bit/peewee-async ?

Sure. Peewee-async uses another approach, with a centralized manager object.

@kszucs Neat. Does aiopeewee only support Postgres right now?

Only Mysql, but postgres support is easy to implement - need to port only a couple of methods.
Issue: Add `RetryOperationalError` support for `playhouse.db_url.connect`.
Author: futursolo
State: closed
Created: 2017-05-05 06:32:47

Currently, I am working on a project using peewee object model built against both MariaDB(for production) and SQLite(for testing). Using `playhouse.db_url.connect` seems like to be the best solution.

However, when using the MariaDB backend, after a certain amount of time, the service starts to constantly output `Error 2006: MySQL server has gone away` until I restart the service. After some researches, I found that the documentation says it can be mitigated by adding `RetryOperationalError` mixin to the database class, but it cannot be used with `playhouse.db_url.connect`.

Please add `RetryOperationalError` support for `playhouse.db_url.connect`.

You can do this yourself. I assume you have a MySQLDatabase subclass that you intend to use.... You can, then:

```python

from playhouse.db_url import connect, register_database

register_database(MySQLDatabaseSubClass, 'mysqlretry')
db = connect('mysqlretry://....')
```
Issue: Better Error message
Author: MinchinWeb
State: closed
Created: 2017-05-04 20:21:35

I managed to break peewee, but the error message turnout to be be very unhelpful. Ultimately, the issue was I had model with a field and a property of the same name.

Simplified model:

```python
from peewee import CharField, Model

class BaseCustomer(Model):
    city = CharField(null=True)
    # a bunch of other fields

    # about 100 lines of code

    @property
    def city(self):
        return None
```

If you try and add a record with a value for `city`, the error that comes up is `AttributeError: can't set attribute`. This is technically true, but not particularly helpful in figuring out what went wrong (I had to monkey-patch peewee to figure it out myself).

Would it be possible to add a more helpful error message? If detectable, a warning about having a field and a property would have helped me fix my error in a couple of minutes. If nothing else, even just saying what key peewee isn't able to set would be helpful.

P.S. Thanks for peewee; it's been a great help in my projects and is much appreciated!

P.P.S. I'm using peewee v 2.9.2, and the stacktrace I got points to line 4861 of `peewee.py`

Unfortunately I don't think so in this case. The error is coming from the fact that the `property` has no setter. The property you've defined overrides the other class attribute, so I'm not even sure how to detect it.
Issue: Huge bug - needs high priority
Author: Chemmic
State: closed
Created: 2017-05-02 06:40:27

Hello, 
Im using ur program since 2 years now, I figured out that there exists a huge bug wich fucks the whole programe over.
You really Need to work on it and fix it asap.
The program is really unusable without Fixing it so please get your lazy ass off and fix it.

- Chemmic

LOL. I'm sorry your shit got fucked so hard. Peewee doesn't fuck very much but when it does it fucks **hard**.

Can you tell me about the bug?

Please comment with some details and I'll re-open for discussion.