akumoth/peewee-issues huggingface dataset:
Issue: apsw driver rejecting datetime object
Author: rlander
State: closed
Created: 2015-11-27 21:37:39

I have a model that looks like this:

``` python
class Log(Model):
    name = CharField()
    platform = CharField(null=True)
    language = CharField(null=True)
    created = DateTimeField(default=datetime.datetime.now)
```

When I try to insert a new record

``` python
Log.create(name=agent.name, platform=agent.platform, language=agent.language)
```

I get the following error:

``` bash
File "/Users/rlander/.pythonbrew/venvs/Python-2.7.9/app/lib/python2.7/site-packages/playhouse/apsw_ext.py", line 95, in _execute_sql
    cursor.execute(sql, params or ())
TypeError: Bad binding argument type supplied - argument #7: type datetime.datetime
```

When I remove the "created" field from the Log class or use the default sqlite driver, I get no errors.


```
Python version: 2.7.9
Sqlite version: 3.9.2

peewee==2.7.3
apsw==3.9.2-r1


```


You need to use the `DateTimeField` object from the `apsw_ext` module.

``` python
from playhouse.apsw_ext import DateTimeField
```
Issue: Unexpected parentheses required when using multiple WHERE conditions
Author: martinburch
State: closed
Created: 2015-11-24 23:59:39

I would expect to be able to write:

```
where(
    (
    modelObject.field_name.is_null(True) |
    modelObject.field_name == ""
    )
)
```

but I must add parentheses around `modelObject.field_name == ""` or the query will be generated improperly. The improperly generated query is essentially

```
WHERE `t1`.`field_name` IS NULL = ''
```

which is exactly the opposite of what I want, returning all records that are not null.

For more discussion, please see:
http://stackoverflow.com/questions/33905987/why-is-peewee-not-nesting-this-where-clause-correctly/33906132


Peewee leans on pythons parser to generate expressions. Because of pythons operator precedence you need parentheses around most comparisons. No magic here, just python.


And yet I can do this without needed extra parens:

```
.where(
    (modelObject.field_name << ['a','b','c'] |
    modelObject.field_name % "%word%")
    )
```

Seems inconsistent. I think this needs to be explained in the docs. At least "you should use parentheses around all comparisons, just to be safe." But a list of situations would be ideal.


What do you suggest? Best practice is just put parentheses around binary expressions, then you won't have any issues.


You can created the equivalent expressions by calling the appropriate doubles underscore method on the field object. You can also instantiate expression objects by hand.


The use of parentheses in your python code won't affect the SQL being generated, improves readability for complex, mutli-clause queries (as I typically assume you line-break each expression), and leaves no room for error.

http://docs.peewee-orm.com/en/latest/peewee/querying.html?highlight=precedence#expressions


Sorry, I think you've already got a note in the docs to this effect.

> Note that the actual comparisons are wrapped in parentheses. Python’s operator precedence necessitates that comparisons be wrapped in parentheses.

Thanks for your additional thoughts on this solidly closed issue.
Issue: AttributeError: 'ModelOptions' object has no attribute 'get_sorted_fields'
Author: pawl
State: closed
Created: 2015-11-21 05:40:25

I noticed `get_sorted_fields()`, `get_field_names()`, and `get_fields()` were removed by these commits: https://github.com/coleifer/peewee/commit/2619fcc12b01e34933861de51918507d118655c4 and https://github.com/coleifer/peewee/commit/50c9c1dd24b7a4c48a396a9f5e88f3ea1647859a

It's causing some flask-admin tests to fail, with this error:

```
AttributeError: 'ModelOptions' object has no attribute 'get_sorted_fields'
```

What's the most backwards compatible replacement for get_sorted_fields()? model._meta.sorted_fields?


I'd look into updating the code to use `.sorted_fields`. there is also `.sorted_field_names`. This is a performance improvement, as that code had potential to be called a _lot_.


@rudyryk has a good suggestion here for a temporary solution: https://github.com/flask-admin/flask-admin/issues/1117

```
    for n, f in zip(model._meta.sorted_field_names,
                    model._meta.sorted_fields):
```


Folks, I'm still getting this error.

Traceback:
```
 File "app.py", line 30, in <module>
    admin.add_view(ModelView(database.Comic))
  File "/usr/local/lib/python3.5/dist-packages/flask_admin/contrib/peewee/view.py", line 144, in __init__
    menu_icon_value=menu_icon_value)
  File "/usr/local/lib/python3.5/dist-packages/flask_admin/model/base.py", line 584, in __init__
    self._refresh_cache()
  File "/usr/local/lib/python3.5/dist-packages/flask_admin/model/base.py", line 653, in _refresh_cache
    self._list_columns = self.get_list_columns()
  File "/usr/local/lib/python3.5/dist-packages/flask_admin/model/base.py", line 729, in get_list_columns
    columns = self.scaffold_list_columns()
  File "/usr/local/lib/python3.5/dist-packages/flask_admin/contrib/peewee/view.py", line 163, in scaffold_list_columns
    for n, f in self._get_model_fields():
  File "/usr/local/lib/python3.5/dist-packages/flask_admin/contrib/peewee/view.py", line 152, in _get_model_fields
    return model._meta.get_sorted_fields()
AttributeError: 'ModelOptions' object has no attribute 'get_sorted_fields'
```

Versions:
```
Flask==0.12.2
Flask-Admin==1.1.0
peewee==2.10.1
wtf-peewee==0.2.6
```

What is going on?
Issue: make code more beautiful
Author: olivetree123
State: closed
Created: 2015-11-21 04:25:33

The 'Meta class' makes code ugly,would you prepare to make code like this:

class Gym(Model):
    **db_table** = 'gym'
    id   = IntegerField(primary_key=True)


No, that has been that way since version 0.1.0. Are you serious? Do you think I'd actually change it?


Also, cluttering up the namespace is grosser to me.


Also bite me.
Issue: Fail to install peewee under WinPython
Author: mindv0rtex
State: closed
Created: 2015-11-20 23:24:31

Hi guys. I am trying to install peewee under WinPython. It comes with Cython pre-intalled, so peewee attempts to compile extensions and this is where the install fails. It complains about sqlite3.h absence and I don't know how to resolve this. Compiling sqlite3 and putting the .a file in the lib folder did not help... I asked the same question on stackoverflow and received a comment which I cannot parse...

http://stackoverflow.com/questions/33834113/fail-to-install-peewee-under-winpython


Hmm, well this isn't exactly a solution, but I've released a new versoin 2.7.2 that does not build SQLite extension by default.

If you want the sqlite extension it should be built using `python setup.py build_sqlite_ext`.

I will look into checking the python version and renaming the entry-point per the stackoverflow answer. (`PyInit__sqlite_ext`).

Fuck python 3 and it's stupid ass changes.


FWIW, I confirm that I could successfully perform the `python setup.py build_sqlite_ext` under WinPython 2, so this indeed looks like a Python 3 issue.


Some issues with the `_sqlite_ext` extension:
1. On Python 3, the extension module fails to link. The module's init function is actually named `PyInit__sqlite_ext`, not [`init_sqlite_ext`](https://github.com/coleifer/peewee/blob/master/playhouse/_sqlite_ext.pyx#L289)
2. On Python 3 for Windows, the [`sqlite3_sqliteext_init`](https://github.com/coleifer/peewee/blob/master/playhouse/_sqlite_ext.pyx#L283) function is not exported from the module. I think this is a Cython bug.
3. On Windows, the [`load_extension`](https://github.com/coleifer/peewee/blob/master/playhouse/sqlite_ext.py#L711) function fails to load the `_sqlite_ext` module because the file extension of the Python extension module is `.pyd`, not `.dll`. Also, on Python 3.5, the name of the Python extension is `_sqlite_ext.cp35-win_amd64.pyd` or `_sqlite_ext.cp35-win32.pyd`, not `_sqlite_ext.pyd`


Hello, encountered this problem again (sorry for the duplicate issue). Would there be a fix, other than to not use peewee with Py3.X on windows?


@shirish93 can you please paste the traceback?


I solved the issue (probably more #831) with my python 3.5 setup by insalling VS 2015 with the instructions of this thread:

http://stackoverflow.com/questions/33323172/vcvarsall-bat-needed-for-python-to-compile-missing-from-visual-studio-2015-v-1
Issue: Escaping `%` for parameterized queries
Author: skatenerd
State: closed
Created: 2015-11-20 21:22:43

When I try to run the trigram Postgres query:
`db.execute_sql("select * from person where person.name % 'bobby'")`

The query crashes with `tuple index out of range` because it thinks the `%` is an argument

I'm wondering if there's a way to escape the `%` sign, so that can put the `%` in the query safely.  If I try to pass it in as an argument to the query, it gets wrapped in a string and the query fails:
`db.execute_sql("select * from person where person.name %s 'foo'", ("%",))`


Just use `%%` instead of `%`
Issue: How to make the result to be dict ?
Author: olivetree123
State: closed
Created: 2015-11-20 08:26:24

g = Gym.get()

I want to make variable g to be a dict , like g = dict(g) , how could I do ?


First off this is completely inappropriate for a GitHub issue. Questions go on stack overflow, irc or the mailing list.

Second, to answer, write:

Gym.select().dicts().get()
Issue: The build depends on Cython, not noted in `setup.py`
Author: skatenerd
State: closed
Created: 2015-11-20 05:27:42

If you don't have Cython, this line:
https://github.com/coleifer/peewee/blob/master/setup.py#L12

Sets `cmdclass` to `None`, which causes the (pip) installation to crash, when `dist.py` tries to iterate over `self.cmdclass`:
https://github.com/jaraco/setuptools/blob/master/setuptools/dist.py#L425

Should we just add an `install_requires` argument when we call `setup`?
https://github.com/coleifer/peewee/blob/master/setup.py#L21


Fixed with the 2.7.1 release. Cython is not required, but if you have it peewee will build some c extension.
Issue: querying.rst: update ManyToManyField module belong
Author: franciscod
State: closed
Created: 2015-11-14 05:12:03

ManyToManyField is now on the playhouse.fields module, docs were slightly misleading


Is fields the right ref?


No sir! It's `extra-fields`! fixing that up, sorry!


push-forced the changes, but i've not yet built the docs locally so this is untested


installed sphinx, `make html`, links are working! should I check anything else?


Thanks a lot!
Issue: When using playground.fields.ManyToManyField, I get "no such table: firstmodel_secondmodel_through"
Author: franciscod
State: closed
Created: 2015-11-14 05:08:17

Am I missing something silly ***** ? I've already done create_tables on both models (in the same order that they're defined as classes).

I can post a minimal reproduction file if that's useful!

***SPOILERS: YES I WAS**


(edited the title so model names are general, mines are `TelegramChat` and `TwitterUser`)


I'm stupid:

> We still need a junction table to store the relationships between students and courses. This model can be accessed by calling the get_through_model() method. This is useful when creating tables.

http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#ManyToManyField

search for "creating tables"
Issue: How to design models in a ManyToManyField with a through_model
Author: bcthomas
State: closed
Created: 2015-11-10 18:15:59

(new to peewee/python orms, coming from a "legacy" rails database)

I'm trying to model 2 elements in a many to many relationship, using a join table;
I have a User and a Project, and a join table called ProjectUser.

I've tried this, and I'm getting circular dependency errors because as is ProjectUser is not defined in Project, and if I swap where I define each, then Project isn't defined in ProjectUser:

```
# models/projects.py
from peewee import *
from playhouse.fields import ManyToManyField

from . import users as u

class Project(BaseModel):
  name = CharField(null=True)
  users = ManyToManyField(u.User, related_name='projects', through_model=ProjectUser)

  class Meta:
    db_table = 'projects'

class ProjectUser(BaseModel):
  project = ForeignKeyField(Project, db_column='project_id', index=True, null=True)
  user = ForeignKeyField(u.User, db_column='user_id', index=True, null=True)

  class Meta:
    db_table = 'project_users'

# models/users.py
class User(BaseModel):
  email = CharField(unique=True)
```

When I let peewee determine the table name (by removing the through_model param), it seems to be ok, but obviously my join table isn't named correctly that way.

Can the ForeignKeyField declarations in the ProjectUser model be defined dynamically so I can avoid the circular dependency? Or phrased differently, how would I accomplish this with a custom-named through_model table?

or or... am I just not thinking about this correctly?

TIA!

ps all my models were generated by using pwiz on my rails database.


You can use the `Proxy` object [see circular foreign keys](http://docs.peewee-orm.com/en/latest/peewee/models.html#circular-foreign-key-dependencies).

``` python
class User(BaseModel):
  email = CharField(unique=True)

ProjectUserProxy = Proxy()

class Project(BaseModel):
  name = CharField(null=True)
  users = ManyToManyField(
    User, 
    related_name='projects', 
    through_model=ProjectUserProxy)

  class Meta:
    db_table = 'projects'

class ProjectUser(BaseModel):
  project = ForeignKeyField(Project, db_column='project_id', index=True, null=True)
  user = ForeignKeyField(User, db_column='user_id', index=True, null=True)

  class Meta:
    db_table = 'project_users'

ProjectUserProxy.initialize(ProjectUser)
```

Everything worked correctly with the above code when I tried creating some users, projects, and the relationships between them. The custom through table appeared to be working as well.
Issue: AttributeError: 'bool' object has no attribute 'is_pk' in migrator
Author: kramer65
State: closed
Created: 2015-11-10 17:09:56

I've successfully used the peewee migrator to add and drop columns, and today I wanted to use the migrator for the first time to drop a not null constrain on a column. In my model I added and set the null constraint to `True`:

```
ticket = ForeignKeyField(Ticket, related_name='sent_to_email_addresses', null=True)
```

So I wrote the following lines to do the migration:

```
from playhouse.migrate import MySQLMigrator, migrate
migrator = MySQLMigrator(db.database)
migrate(migrator.drop_not_null('emailsending', 'ticket'))
```

But I'm getting the following error:

```
File "./migrate_db.py", line 55, in from7To8
    migrate(migrator.drop_not_null('emailsending', 'ticket'))
  File "/usr/local/lib/python2.7/dist-packages/playhouse/migrate.py", line 579, in migrate
    operation.run()
  File "/usr/local/lib/python2.7/dist-packages/playhouse/migrate.py", line 144, in run
    getattr(self.migrator, self.method)(*self.args, **kwargs))
  File "/usr/local/lib/python2.7/dist-packages/playhouse/migrate.py", line 152, in inner
    return fn(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/playhouse/migrate.py", line 390, in drop_not_null
    if column.is_pk:
AttributeError: 'bool' object has no attribute 'is_pk'
```

Does anybody know what I'm doing wrong here?


The problem, not obvious at all, is that the `ticket` column is not found. I bet that if you change it to `ticket_id` you will be set:

``` python
from playhouse.migrate import MySQLMigrator, migrate
migrator = MySQLMigrator(db.database)
migrate(migrator.drop_not_null('emailsending', 'ticket_id'))
```


Looking at the tests, there may be some additional issues with the mysql migrations regarding dropping constraints on foreign keys. Looking into it.


@coleifer - Seeing that you closed this issue I presume that after you looked into it you concluded that there were no additional issues with the mysql migrations regarding dropping constraints on foreign keys?

The reason I'm asking is that I'm using this in beta/production now and I wanted to wait with migrating the db until you had a conclusion.


Just to be safe I added another testcase adding and dropping not null constraints to a foreign key to verify they worked.


Thanks for adding the tests. I tested it on 3 test setups before deploying the migration to the production server, and everything went perfectly fine.

Thanks for creating peewee!
Issue: Add connection management documentation for Falcon
Author: arpankapoor
State: closed
Created: 2015-11-06 16:16:48

Middleware components in Falcon framework (http://falconframework.org/)
can be used to add database connection management.


Does the response middleware method not need to return anything? (Or the request middleware for that matter)


Nothing needs to be returned.

The middleware methods may modify the request and response objects passed in as parameters. These objects are routed to the appropriate responder method by Falcon.

See the docstring of the middleware methods [here](https://falcon.readthedocs.org/en/latest/api/middleware.html#middleware-components). 


Thanks!


Not sure if it should be documented, but calling db.close in the process_response kills the cursor, and then a response via stream will fail

A poor man workaround in this case could be to check the existence of the stream:

```
    def process_response(self, req, resp, resource):
        if resp.stream==None and not db.is_closed():
            db.close()
```

And then use some stream incorporating a close() method, which falcon will call.

```
class closingMap(map):
  def close(self):
    db.close()
```
 for python > 3.6 some technique more "awaitable" could be better
Issue: OperationalError when deferring foreign keys
Author: notdaniel
State: closed
Created: 2015-11-04 19:28:13

I've been trying to use the Proxy() trick, followed by its initialization, then table creation, and finally foreign key creation, exactly as shown in the [Circular foreign key dependencies](https://peewee.readthedocs.org/en/latest/peewee/models.html?highlight=defer#circular-foreign-key-dependencies) documentation. However, I always receive the following error:

``` python
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/dkh/devel/peeweetest/venv/lib/python2.7/site-packages/peewee.py", line 3436, in create_foreign_key
    model_class, field, constraint))
  File "/home/dkh/devel/peeweetest/venv/lib/python2.7/site-packages/peewee.py", line 3339, in execute_sql
    self.commit()
  File "/home/dkh/devel/peeweetest/venv/lib/python2.7/site-packages/peewee.py", line 3185, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/home/dkh/devel/peeweetest/venv/lib/python2.7/site-packages/peewee.py", line 3331, in execute_sql
    cursor.execute(sql, params or ())
OperationalError: near "CONSTRAINT": syntax error
```

Wondering if it was with my models or something else on my end, I have tried using the exact models and code on the documentation with the same result. Any thoughts?


What database are you using?


Ha! I was using SQLite (which I usually use when starting something out) but after your response, I just tried it with Postgres and it worked fine. Is this a SQLite limitation, then? Either way, this is helpful since I was going to move this to Postgres eventually anyway.


SQLite does not support adding constraints. So basically you can follow the documentation example and simply skip the call to add the constraint. I've added a test and updated the docs to make this clear.
Issue: `model_to_dict`'s `extra_attrs` only applies to the top-level object in the case where `recurse=True`
Author: josefdlange
State: closed
Created: 2015-11-03 22:42:02

Might I suggest a couple of solutions:

1) Have `extra_attrs` actually take in the attributes as the list members -- so instead of:

```
model_to_dict(x, extra_attrs=['method_bar', 'property_foo']
```

You would see:

```
model_to_dict(x, extra_attrs=[ClassX.method_bar, ClassX.property_foo])
```

This interface is more familiar and consistent with the `exclude`, `only`, etc parameters of the same function.

Is there a particular reason why this one is purely string-based?


Immediately after posting it I of course thought of the way a method or property lives on the class level, and how it might be difficult to reconcile to an instance's bound method. Hmm...


I'm seeing the difficulty here. If you pass in the unbound method, there's no (apparent) way to match a bound instance method with said unbound class method. If there's no way you can figure out either, I'll probably close this one up.


@coleifer Which means itsn't possible to get `extra_attrs` on recursive items?

No, for such cases as that you're probably best off just writing your own serializer.
Issue: Error when using JSONField() with case()
Author: scribu
State: closed
Created: 2015-11-02 17:51:51

Here's my simplified model:

``` python
from peewee import *
from playhouse.postgres_ext import *

db = PostgresqlExtDatabase('mobster')

class Posting(BaseModel):
    class Meta:
        database = db

    price_info = JSONField()
```

And here's a query I'm trying to make:

``` python
from playhouse.shortcuts import case

ONE_EURO_IN_RON = 4.4407

price = case(
        Posting.price_info['currency'],
        (
            ('EUR', Posting.price_info['value']),
            ('RON', Posting.price_info['value'] / ONE_EURO_IN_RON)
        )
).alias('price')

query = Posting.select(price)

postings = list(query)
```

And here's the error:

```
DataError: invalid input syntax for type numeric: "value"
LINE 1: ...o"->>'value' WHEN 'RON' THEN ("t1"."price_info"->>'value' / ...
```

Full query info:

```
<class 'Posting'> SELECT CASE "t1"."price_info"->>%s WHEN %s THEN "t1"."price_info"->>%s WHEN %s THEN ("t1"."price_info"->>%s / %s) END AS price FROM "posting" AS t1 ['currency', 'EUR', 'value', 'RON', 'value', 4.4407]
```

Versions:
- Postgres 9.4.4
- psycopg2 2.6.1
- peewee 2.6.4


Postgres is actually right in this case, and this isn't a bug in peewee. It looks like the value returned by the attribute lookup is not numeric. You can cast it, but you have to cast both return values it seems.

One way that seemed to work was:

``` sql
SELECT 
CASE "t1"."price_info"->>'currency' 
WHEN 'EUR' THEN ("t1"."price_info"->>'value')::float 
WHEN 'RON' THEN (("t1"."price_info"->>'value')::float * 1.5) 
END AS price 
FROM "xxtest" AS t1;
```

To accomplish this with peewee, you can do something like this. It's not super elegant, but it should work.

``` python
from peewee import Clause, SQL, fn

def to_float(node):
    return fn.CAST(Clause(node, SQL('AS float')))

price = case(
    Posting.price_info['currency'],
    (
        ('EUR', to_float(Posting.price_info['value'])),
        ('RON', to_float(Posting.price_info['value']) * 1.5),
    )
).alias('price')
```


With 73cfdc1824f4161db062fc74fc77a5a13660888e it should be possible to now write:

```
price = case(
        Posting.price_info['currency'],
        (
            ('EUR', Posting.price_info['value'].cast('float')),
            ('RON', Posting.price_info['value'].cast('float') / ONE_EURO_IN_RON)
        )
).alias('price')
```


Works! Thanks for the syntax sugar.