akumoth/peewee-issues huggingface dataset:
Issue: Error when I run:- run_example in the twitter app
Author: Doctor-Foxling
State: closed
Created: 2017-07-31 23:18:33

Hi,
I'm new to peewee and when I run the run_example in the example twitter app I get an error somewhere in the line 131 but can't seem to find what exactly.

Without you sharing the full traceback I have no way of helping you. Are you new to python?

Here's what I get. I should have shared the traceback yes, but I ran the code as it was without changing anything so I thought something might have gone wrong after one of the commits. Btw, is there a better way of sharing the traceback. Cheers!

![1](https://user-images.githubusercontent.com/30374624/28891709-5e7eee6a-77c3-11e7-9f02-e702a412dd6e.JPG)
![2](https://user-images.githubusercontent.com/30374624/28891710-5e7f201a-77c3-11e7-8719-f7766e8283f4.JPG)




In the docs, there is a section on explicitly creating the tables: http://peewee.readthedocs.io/en/latest/peewee/example.html#creating-tables

The docs read:

> In order to start using the models, its necessary to create the tables. This is a one-time operation and can be done quickly using the interactive interpreter. We can create a small helper function to accomplish this:

```python
def create_tables():
    database.connect()
    database.create_tables([User, Relationship, Message])
```

> Open a python shell in the directory alongside the example app and execute the following:

```pycon
>>> from app import *
>>> create_tables()
```

So you'll `cd` into the twitter example and open a python interpreter and run the above 2 lines of code.

Hi Charles,

I appreciate the help but wouldn't it be better if the actual code in the
example had the create_tables() called in the __name__ == '__main__'
section, so every time someone tripped at this problem, they wouldn't
bother you for it.

On Thu, Aug 3, 2017 at 1:00 AM, Charles Leifer <notifications@github.com>
wrote:

> Closed #1323 <https://github.com/coleifer/peewee/issues/1323>.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/coleifer/peewee/issues/1323#event-1190532760>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/Ac964H883O6BXVGzlYWihwtffqAZLWzfks5sUQ2ggaJpZM4OpCwD>
> .
>
Issue: UpdateQuery FROM clause support
Author: rammie
State: closed
Created: 2017-07-28 20:44:47

Turns out postgresql's Update statements support a FROM clause that is useful for doing bulk updates. I don't think this is standard SQL.

I looked through the documentation and couldn't find any examples of it. I perused the generate_update function in peewee and didn't see and SQL('FROM')'s in there so I assume that this is not supported.

I found this on the group, which is a related question. https://groups.google.com/forum/#!topic/peewee-orm/ov4M434fZ7U

It would be nice if peewee supported this. I was wondering if you had any suggestions on how to achieve this.

Standard disclaimer: I love peewee! It's my favorite ORM ever.

Doesn't seem worth the trouble. The subquery would likely be cached if it appears twice. Sorry for the noise.

Well, I think it should be added to Peewee. Currently I don't see any ways in Peewee to do what `UPDATE FROM` does — to update one table using a joined table not only in `WHERE` (which could really be done with subqueries), but also as data source for expression in `SET`.

@coleifer, is there any chance that you will add this?

It gets a bit weird...SQLite does not support fully-qualified column names in the SET portion of the query. I *believe* it's possible to accomplish anything you need without the `FROM` clause that would normally be possible using sub-selects or common-table-expressions. Closing.
Issue: ForeignKeyField('self',…) not correctly inheriting?
Author: phryk
State: closed
Created: 2017-07-19 15:58:57

When inheriting from a Model using `ForeignKeyField('self',…)`, the resulting table
references the (possibly inexistent) table of the Model originally defining the key.

I'm not sure whether this is intended behavior but intuitively, I expected the inherited models to also reference themselves, rather than some class further up.

I wrote a bit of code to illustrate what I'm trying to do and where it fails.

Since I haven't seen any good way to attach files to issues, I'm just pasting it verbatim:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import peewee

db = peewee.SqliteDatabase('test.db')

class Forestable(peewee.Model):

    class Meta:
        database = db

    name = peewee.CharField(unique=True)
    parent = peewee.ForeignKeyField('self', null=True)


class SubForestable(Forestable):

    pass

if __name__ == '__main__':

    SubForestable.create_table()

    x = SubForestable()
    x.name = 'foo'
    x.save()

    y = SubForestable()
    y.name = 'bar'
    y.parent = x
    y.save()

    # works fine
    print "X:"
    print x
    print x.name
    print x.parent
    print "--"

    print "Y:"
    print y
    print y.name
    print y.parent
    print "--"

    # works fine up to…
    q = SubForestable.select()
    for z in q:
        print z
        print z.name
        print z.parent # here, where it tries accessing table 'forestable'
```
I tested this with 2.10.1 from pip and the 3.0a branch from git.

Interesting... Out of curiosity I tested this on Django, and found it behaves the same as Peewee in this case.

You should be able to re-define the foreign key on the child table and I'm going to suggest that should be the proper resolution to this issue. Doing some kind of late-late-binding of the foreign key model doesn't really work because it's not clear at what point that binding should be done.
Issue: InternalError: (1054, u"Unknown column 't1.pk' in 'field list'")
Author: guangbaowan
State: closed
Created: 2017-07-14 14:41:11

Hi, here is my code. I used the connection pool and created the table, when use MyModel.select.first(), but returns 'InternalError: (1054, u"Unknown column 't1.pk' in 'field list'")', am I doing something wrong? 
```
from playhouse.db_url import connect
mysql_db = connect('mysql+pool://root:root@127.0.0.1:3306/test?max_connections=20&stale_timeout=300')

from promotion_service.store import MySqlPrimaryKeyAutoIncrementField

class MyModel(Model):
    pk = MySqlPrimaryKeyAutoIncrementField()
    other_field = TextField()
    class Meta:
        db_table = 'my_model'
        database = mysql_db
```

Can you share your definition of MySqlPrimaryKeyAutoIncrementField ?  Also, it should be MyModel.select().first() (your ex has missing parentheses after select).

@coleifer 
.Oh . sorry .(MyModel.select.first(), here is the typo, in my code, I typed the right one  )
```
class MySqlPrimaryKeyAutoIncrementField(BigIntegerField):
    def __init__(self, *args, **kwargs):
        kwargs['primary_key'] = True
        super(MySqlPrimaryKeyAutoIncrementField, self).__init__(*args, **kwargs)

    def __ddl__(self, column_type):
        ddl = super(MySqlPrimaryKeyAutoIncrementField, self).__ddl__(column_type)
        return ddl + [SQL('auto_increment')]
```

I'm really confused. when I use
```
mysql_db = MySQLDatabase('test', host='127.0.0.1', port=3306, user='root', passowrd='root')
```
worked well.

In your `__ddl__` method you're not returning anything. That's the issue.
Issue: CompositeKey as primary_key raises AttributeError
Author: celestialorb
State: closed
Created: 2017-07-14 09:40:45

I have a very basic model that peewee fails to create the model for (the table is created, but peewee raises an AttributeError.

My model is as shown:

```python
import datetime as dt
import peewee as pew

class BaseModel(pew.Model):
    class Meta:
        database = <DATABASE>

class UpdateBatch(BaseModel):
    collected_at = pew.DateTimeField(default=dt.datetime.now)
    batch_type = pew.CharField(default='DEFAULT')
    class Meta:
        db_table = 'UpdateBatches'
        primary_key = pew.CompositeKey('collected_at', 'batch_type')
```

When starting from a fresh database and use `create_tables`  I receive the following error:

```
AttributeError: 'CompositeKey' object has no attribute 'get_db_field'
```

`UpdateBatch` is the only model that has a `CompositeKey` in my application.

Note: this was using a MySQL database as a backend.
Using version peewee 2.10.1.

I do not encounter any such error running peewee 2.10.1.

Code:

```python
import datetime
from peewee import *


class Base(Model):
    class Meta:
        database = SqliteDatabase(':memory:')


class UpdateBatch(Base):
    timestamp = DateTimeField(default=datetime.datetime.now)
    batch_type = CharField(default='DEFAULT')

    class Meta:
        db_table = 'update_batches'
        primary_key = CompositeKey('timestamp', 'batch_type')


Base._meta.database.create_tables([UpdateBatch])
```

Have you tried it with a MySQL backend?  Looks like you're using Sqlite.

No, I don't have MySQL handy at the moment. Can you try the above code with SQLite, however? I'd like to verify if this is a peewee issue or a peewee/mysql issue.

Can do, please hold.

Tried your code on both Sqlite and MySQL (fresh Docker container) and didn't encounter any issues.  Will reexamine my code / test environment to see if I can find a difference.

Looks like the issue is coming from my other classes in my model, which are using a `ForeignKeyField` to reference the `CompositeKey`.  Looks like a duplicate of #270.
Issue: db_table does not work
Author: guangbaowan
State: closed
Created: 2017-07-14 04:24:32

Hi, guys, I used peewee to build models .
```
class BaseModel(Model):
    class Meta:
        # Mysql connection
        database = DB

class PromotionUser(BaseModel):
    created_at = DateTimeField(default=datetime.datetime.utcnow, index=True)
    # seq_id = PrimaryKeyField(null=False)
    source_uid = CharField()
    source = CharField()
    class Meta:
        db_table = 'promotion_user'
```
PromotionUser.create_table(). the table name is 'promotionuser', not 'promotion_user', could someone plz help?
Issue: peewee.OperationalError: (1060, "Duplicate column name )
Author: iwwxiong
State: closed
Created: 2017-07-13 10:14:30

Consider the following models:

```python
class Author(Model):
    id = PrimaryKeyField()
    name = CharField(max_length=50)

    class Meta:
        db_table = 'Author'

class Book(Model):
    id = PrimaryKeyField()
    name = CharField(max_length=255)
    author = ForeignKeyField(Author)

    class Meta:
        db_table = 'Book'
```

Follow the models, I execute this query:

```python
Book.select(Book.id, Author.id).join(Author).limit(10).count()
```

some error throw `peewee.OperationalError: (1060, "Duplicate column name 'id')`. then i check the query sql like this:

```sql
SELECT COUNT(*1) FROM (SELECT `t1`.`id`, `t2`.`id` FROM `Book` AS t1 INNER JOIN `Author` AS t2 ON (`t1`.`author_id` = `t2`.`id`) LIMIT 10) AS wrapped_select
```

So, i konw this error is SQL as alias table SQL syntax error. But i think this case, we can empty the select conditions then excute count.

Try aliasing one of the columns, Book.id.alias('book_id') or Author.id.alias('author_id').
Issue: N+1 avoidance not working?
Author: c17r
State: closed
Created: 2017-07-12 21:35:55

My models are
```
class BaseModel(peewee.Model):
    class Meta:
        database = database


class Group(BaseModel):
    name = peewee.CharField(max_length=255)
    reddit_name = peewee.CharField(max_length=30)
    added = peewee.DateTimeField(default=_now)
    member_count = peewee.IntegerField(default=0)

    class Meta:
        indexes = (
            (('name', 'reddit_name'), True),
        )


class Member(BaseModel):
    group = peewee.ForeignKeyField(Group, related_name='members', index=True)
    reddit_name = peewee.CharField(max_length=30)
    added = peewee.DateTimeField(default=_now)
```

Trying to avoid the N+1, I do
```
>>> group = (Group
            .select(Group, Member)
            .join(Member)
            .where(Group.name == 'test-group', Group.reddit_name == 'c17r')
            .order_by(Member.reddit_name)
            .get())
```

If I turn on logging, I can see that it did what I wanted to:
```
[2017-07-12 17:29:45.128426] DEBUG: peewee: ('SELECT "t1"."id", "t1"."name", "t1"."reddit_name", "t1"."added", "t1"."member_count", "t2"."id", "t2"."group_id", "t2"."reddit_name", "t2"."added" FROM "group" AS t1 INNER JOIN "member" AS t2 ON ("t1"."id" = "t2"."group_id") WHERE (("t1"."name" = ?) AND ("t1"."reddit_name" = ?)) ORDER BY "t2"."reddit_name" LIMIT 1 OFFSET 0', ['test-group', 'c17r'])
```

But,
```
>>> group.members
<class 'data.Member'> SELECT "t1"."id", "t1"."group_id", "t1"."reddit_name", "t1"."added" FROM "member" AS t1 WHERE ("t1"."group_id" = ?) [1]
>>> 
>>> [m.reddit_name for m in group.members.iterator()]
[2017-07-12 17:30:25.148410] DEBUG: peewee: ('SELECT "t1"."id", "t1"."group_id", "t1"."reddit_name", "t1"."added" FROM "member" AS t1 WHERE ("t1"."group_id" = ?)', [1])
['one', 'two', 'three', 'four']
>>>
```

Did I miss something on the N+1 avoidance?

You're going the "wrong way" when following that query. You'll need to use `prefetch()` instead.

http://docs.peewee-orm.com/en/latest/peewee/querying.html#list-users-and-all-their-tweets

I'd suggest giving the docs on N+1 a close read. The approach you're using works fine for getting an object and a singular related instance (i.e., would work fine for listing all members and a given group). But when you have a single instance and want to get all objects that are related to it, you need to use `prefetch()`.
Issue: Relation already exists error when using Model's create_table with fail silently option
Author: Tumetsu
State: closed
Created: 2017-07-12 17:08:35

Hello, I'm using peewee with peewee_migrate library and encountered a problem today. 
When I try to run my migrations, I get error
```
peewee.ProgrammingError: relation "migratehistory" already exists
```
More info in issue I posted to peewee_migrate's repo: https://github.com/klen/peewee_migrate/issues/51
I investigated the reason and found the following. In the peewee_migrate, the migration history is created in a following function:
```
@cached_property
    def model(self):
        """Initialize and cache MigrationHistory model."""
        MigrateHistory._meta.database = self.database
        MigrateHistory._meta.db_table = self.migrate_table
        MigrateHistory.create_table(True)
        return MigrateHistory
```
Note the `create_table` call. After tinkering around a bit I noticed that the next version works:
```
@cached_property
    def model(self):
        """Initialize and cache MigrationHistory model."""
        MigrateHistory._meta.database = self.database
        MigrateHistory._meta.db_table = self.migrate_table
        self.database.create_table(MigrateHistory, safe=True)
        return MigrateHistory
```
You see I use `database` object's class method instead of `Model`s.

This led me to wonder if the problem is actually in peewee and model's create_table won't work as expected. Of course it might be an issue with peewee_migrate (and therefore not really your business) as I originally suspected but I'd like to hear if you have any idea why the original code won't work or if there is indeed a bug in peewee :)

Finally here is link to peewee_migrate's file containing the snippets above: https://github.com/klen/peewee_migrate/blob/develop/peewee_migrate/router.py

And thanks by the way for creating peewee!



Found the problem which was in peewee_migrate. So nothing to see here. More information for curious: https://github.com/klen/peewee_migrate/issues/51
Issue: Peewee ignoring db_table on create_tables()
Author: timlehr
State: closed
Created: 2017-07-07 13:12:17

Hi Charles,

first: thanks for your hard work on peewee. It's a lovely package that already saved us a lot of time. :+1: 

However, we have an issue similiar to #201 where our uppercase db names are converted to lowercase, even though we specify an explicit uppercase name in the `db_table` attribute of the `meta` class.

```
from luti.core.models.basemodel import BaseModel

from peewee import *

class Collection(BaseModel):
	filter = CharField()
	id = PrimaryKeyField(db_column='idCollection')
	longname = CharField(null=True)
	path = CharField()

	class Meta:
		db_table = 'Collection'
```

Whatever we put in `db_table` will be used when we create the tables with `create_tables()`, but it will ignore any uppercase characters and lowercase them. We are using Python 2.7, Peewee 2.10.0 and MariaDB 10.1.24 / MySQL 5.7.18 servers for our test databases. Any clues?

It appears to be working as I'd expect. In the example below the database is reporting a table named "Foo" and the SELECT query is querying the "Foo" table (successfully).

```python
In [1]: from peewee import *

In [2]: db = SqliteDatabase('/tmp/foo.db')

In [3]: class Foo(Model):
   ...:     value = TextField()
   ...:     class Meta:
   ...:         db_table = 'Foo'
   ...:         database = db
   ...:         

In [4]: db.create_tables([Foo])

In [5]: db.get_tables()
Out[5]: [u'Foo']

In [6]: Foo.create(value='x')
Out[6]: <__main__.Foo at 0x7fdd08157ed0>

In [7]: Foo.select()
Out[7]: <class '__main__.Foo'> SELECT "t1"."id", "t1"."value" FROM "Foo" AS t1 []

In [8]: Foo.get()
Out[8]: <__main__.Foo at 0x7fdd0815eb10>

```

Hi Charles, 
I found the issue after trying the deployment on a Linux machine. 
MySQL on Windows makes DB table names lowercase by default (for some reason).
You have to manually change that option in the ini-file.

Sorry for the trouble!

Cheers,
Tim
Issue: Certain operations not respecting the db_value()
Author: TheLarsinator
State: closed
Created: 2017-07-07 08:42:58

I saw that this was an issue before, #150 

But I experience the same problem, so perhaps this fix has been undone in a later release:

I have made a ListField, which is pretty much the same as here:

```
class ListField(Field):    
 
   def db_value(self, list):
        return str(list)

    def python_value(self, list):
        return ast.literal_eval(list)
```

I get the same error: peewee.OperationalError: near ",": syntax error
db_value doesnt get a list, but gets called for each item in the list

Workaround:
Make a class that contains a list. Make a ClassField for this class, that uses the list to create the output.
Not an optimal solution, but works for now

It seems to be working OK for me:

```python
from peewee import *

class ListField(Field):
    def db_value(self, value):
        return ','.join(value)
    def python_value(self, value):
        return value.split(',')

db = SqliteDatabase(':memory:')

class Register(Model):
    data = TextField()
    items = ListField()
    class Meta:
        database = db

db.create_tables([Register])

r = Register.create(data='foo,bar,baz', items=['foo', 'bar', 'baz'])
r.items  # ['foo', 'bar', 'baz']

r_db = Register.get()
r_db.items  # ['foo', 'bar', 'baz']
```

Ok, so I think I narrowed it down to the get_or_create method. 

r, c = Register.get_or_create(data='foo,bar,baz', items=['foo', 'bar', 'baz'])

Use that to recreate

When doing a `SELECT`, peewee doesn't know if the list you are passing should be unpacked (as it would for an `IN` query, for example). To work around, when passing lists in this situation you should wrap it in Param:

```python
r = Register.select().where(Register.items == Param(['foo', 'bar', 'baz']))
```

In Peewee 3.0a, you can accomplish the same `AsIs`.

Problem solved :) Thank you for the quick help!
Issue: Swap databases to models
Author: jbremson
State: closed
Created: 2017-07-06 19:21:58

I would like to test my models against a test db, but I'm not seeing an easy way to swap the db handle in the models.

How do I do this? 

I figured this out after some digging in the code. See my PR for a small change to the docs. 

It would be nice if there was a way to change the db handle on an application wide basis.  The Using context manager loses scope when calling outside functions. 

I will take a stab at trying to implement this. Any advice on how to do it? 

> It would be nice if there was a way to change the db handle on an application wide basis.

You can always implement an abstraction layer that handles this kind of thing. Or use the `Proxy` object:

```python
db = Proxy()
db_1 = SqliteDatabase('one.db')
db_2 = SqliteDatabase('two.db')

class MyModel(Model):
    class Meta:
        database = db

class AnotherModel(Model):
    class Meta:
        database = db

db.initialize(db_1)  # Now MyModel and AnotherModel are using db_1

# do some stuff....

db.initialize(db_2)  # Now they're using db_2...
```
Issue: No such module: FTS4
Author: Billmike
State: closed
Created: 2017-07-04 14:32:44

Traceback (most recent call last):
  File "app.py", line 88, in <module>
    main()
  File "app.py", line 84, in main
    database.create_tables([Entry, FTSEntry], safe=True)
  File "C:\Users\Billmike\Documents\charlesblog\blog\lib\site-packages\peewee.py", line 3855, in create_tables
    create_model_tables(models, fail_silently=safe)
  File "C:\Users\Billmike\Documents\charlesblog\blog\lib\site-packages\peewee.py", line 5294, in create_model_tables
    m.create_table(**create_table_kwargs)
  File "C:\Users\Billmike\Documents\charlesblog\blog\lib\site-packages\playhouse\sqlite_ext.py", line 243, in create_table
    cls._meta.database.create_table(cls, options=options)
  File "C:\Users\Billmike\Documents\charlesblog\blog\lib\site-packages\playhouse\sqlite_ext.py", line 970, in create_table
    return self.execute_sql(sql, params)
  File "C:\Users\Billmike\Documents\charlesblog\blog\lib\site-packages\peewee.py", line 3775, in execute_sql
    self.commit()
  File "C:\Users\Billmike\Documents\charlesblog\blog\lib\site-packages\peewee.py", line 3598, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "C:\Users\Billmike\Documents\charlesblog\blog\lib\site-packages\peewee.py", line 3768, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: no such module: FTS4

You don't have the SQLite FTS4 extension. You'll need to either compile it as a standalone and load it at run-time, or recompile SQLite with full-text search feature enabled. https://sqlite.org/fts3.html

Hello, I am using Anaconda with Python 3.7, how can I install FTS4 extension?

I'd suggest asking anaconda.
Issue: [idea] New <SelectQuery> method: namedtuples()
Author: ak4nv
State: closed
Created: 2017-07-03 03:26:33

Like `dicts()` or `tuples()`, this method returns a result as tuple of `collection.namedtuple` objects.

Added here: 13c15c3a8e086939184c3b885f3dbb4d057b68a5

This functionality is provided in the 3.0 branch (not yet public) as well.