akumoth/peewee-issues huggingface dataset:
Issue: VarCharColumn - TypeError: 'long' object is not subscriptable
Author: smokedice
State: closed
Created: 2012-04-12 09:48:09

Line 1944 should convert the provided value into a string:
     value = str(value or '')

This should stop errors occurring when integers or such are passed in.

Traceback (most recent call last):
  File "/home/smoky/Documents/Workspace/TASv3/Code/common/CommonSignals/AbstractSignal.py", line 25, in execute
    action(_args, *_requiredKeywords)
  File "/home/smoky/Documents/Workspace/TASv3/Code/master/MasterBusinessLogic/Results/Loggers/SQLLiteLogger.py", line 67, in __conformancePackName
    tasVersion = TASBuildDetails.TASVersion)
  File "/home/smoky/Documents/Workspace/TASv3/Code/libs/peewee/peewee.py", line 2567, in create
    inst.save(force_insert = True)
  File "/home/smoky/Documents/Workspace/TASv3/Code/libs/peewee/peewee.py", line 2601, in save
    new_pk = insert.execute()
  File "/home/smoky/Documents/Workspace/TASv3/Code/libs/peewee/peewee.py", line 1754, in execute
    result = self.raw_execute(*self.sql())
  File "/home/smoky/Documents/Workspace/TASv3/Code/libs/peewee/peewee.py", line 1737, in sql
    cols, vals = self.parse_insert()
  File "/home/smoky/Documents/Workspace/TASv3/Code/libs/peewee/peewee.py", line 1732, in parse_insert
    vals.append(field.db_value(v))
  File "/home/smoky/Documents/Workspace/TASv3/Code/libs/peewee/peewee.py", line 2130, in db_value
    return self.column.db_value(value)
  File "/home/smoky/Documents/Workspace/TASv3/Code/libs/peewee/peewee.py", line 1948, in db_value
    return value[:x]
TypeError: 'long' object is not subscriptable
Issue: PK not respecting db column (via mailing list)
Author: coleifer
State: closed
Created: 2012-04-11 15:13:25

Model:

``` python
class Peer(BaseModel):
    peerId = PrimaryKeyField(db_column = 'id')
    macAddress = CharField()
    netAddress = CharField()
```

Error:

```
Traceback (most recent call last):
  File "/home/smoky/Documents/Workspace/TASv3/Code/tests/BusinessLogic/Results/Loggers/TestSQLLiteLogger.py", line 58, in testOldDataDestroyed
    assert len(Peer.select().count()) == 1
  File "/usr/local/lib/python2.7/dist-packages/peewee-0.9.1-py2.7.egg/peewee.py", line 1325, in count
    res = clone.database.execute(*clone.sql())
  File "/usr/local/lib/python2.7/dist-packages/peewee-0.9.1-py2.7.egg/peewee.py", line 310, in execute
    res = cursor.execute(sql, params or ())
OperationalError: no such column: peerId
```

It looks as if the db_column is being ignored by line 2371. Currently it is using attr.name, but surely it should use attr.db_column ?


Places to test:
- setting up joins (follow_joins)
- select query count()
- parse select query special case for when just an id is selected .select("id") -> .select({Model: ['id']})
- convert_subquery
- annotate_query
- foreign related object lookup
- foreign key field
- model save
- model collect_queries
- delete instance
Issue: SelectQuery dictionary fields
Author: jdearl
State: closed
Created: 2012-04-05 20:07:26

With the following I can access 'min' and 'max', but not 'price1'.

products = Product.select({
    Product: ['*'],
    Item: ['price1', Min('price1'), Max('price1')],
}).where(category_id=id).group_by('id').join(Item)

SQL:

('SELECT t1."id", t1."brand", t1."category_id", t1."name", t1."overview", t1."details", t2."price1", MIN(t2."price1") AS min, MAX(t2."price1") AS max FROM "products" AS t1 INNER JOIN "items" AS t2 ON t1."id" = t2."product_id" WHERE t1."category_id" = ? GROUP BY t1."id" ORDER BY t1."brand" ASC, t1."name" ASC', [u'29'])


First off, thank you so much for including the SQL, makes debugging so much easier :)

Since a product can have multiple items, the value of the aggregate functions has meaning and that is why you get meaningful results.  The big question is what you expect "price1" to be.  By the "?" interpolation I gather you're using sqlite -- trying this query in postgresql would most likely not work since it is a bit stricter.


Yes, it's a bit contrived :)

In the case of the SQLite 3.7.11 it would return the same price as
MAX(price1).  Also, I'm not extremely familiar with PostgreSQL, but I
believe the latest versions allow such queries also.

I'm totally fine if you don't want to allow this.  It was just a
little confusing, because I was able to provide a list of fields to
Product, but not to Item.  I do agree though, price1 isn't really
useful.

Sort of related, would R() work in this case?  For example:

R('GROUP_CONCAT(image) AS image')


Yeah, in my opinion this is one of those things that give ORMs a bad rap.  For a detailed description of a possible solution, check out https://groups.google.com/forum/?fromgroups#!topic/peewee-orm/RLd2r-eKp7w

You're doing an inner join but you want, in a sense, a right join on item and peewee doesn't support this...and as you said, the value of price1 is "indeterminate" but should probably be a list.  The way I would fix this is to query Item and group by product.  I'm not familiar with the group_concat function but would be interested in learning more.


Thanks!
Btw, I didn't realize there was a peewee forum!


If you want to try this out, its kidn of hacky but might do what you want:
http://peewee.readthedocs.org/en/latest/peewee/querying.html#speeding-up-simple-select-queries


That works great, thank you!  Good choice in method name  :)
Issue: ValueError datetime
Author: jdearl
State: closed
Created: 2012-04-04 18:34:14

I have an SQLite database which has a DateTimeField() with a few different datetime formats (not on purpose).  I will probably fix my datetime values, but I thought I'd post this in case you feel peewee should handle this gracefully?

2011-11-15 21:53:59.759451+0000
2011-11-28 20:12:03
2009-01-01 (error)

http://paste.pocoo.org/show/aH7tITRfn9mhczxgWq3u/

Thanks for Peewee (and Flask-Peewee)!


I think the first 2 should probably work as expected with the existing DateTimeField, but the last should probably be given its own "DateField" a-la django (and maybe a TimeField as well for times).  Thanks for reporting


Is it possible to cast them to a DateTime with 0's?  That's sorta what I'd expect to happen.  I'm coming from Ruby, and Sequel or the Ruby SQLite adapter appears to do be doing this.

I also added that the field is defined as a peewee DateTimeField().


Totally, if the DateTimeField encounters something like example 3 it should zero out the times.


Great!


Thanks for fixing  :)


Would be really good if we could pass in the format string into the field constructor. I have dates in a sqlite db of '9 January 2009' so I'd like to pass the format in:

transaction_date = DateField(format='%d %B %Y')

I'll fork and suggest something....


Thanks for suggesting this fix, I've added it here:


here's the commit: 0d9708a0f97685144aa7a9773755832dab459b07

the way it works is you can pass in "formats" kwarg in the field constructor containing a list of various formats to try.


Legend Charles - you beat me to it :)

Love Peewee btw - its my ORM of choice. In fact I don't choose any
other these days - solid.

Thanks for the rapid help.

Ben

On Mon, May 14, 2012 at 5:30 PM, Charles Leifer
reply@reply.github.com
wrote:

> here's the commit: 0d9708a0f97685144aa7a9773755832dab459b07
> 
> the way it works is you can pass in "formats" kwarg in the field constructor containing a list of various formats to try.
> 
> ---
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/coleifer/peewee/issues/68#issuecomment-5694864

## 

Ben Hughes
Change & Innovation Lead
Bright Approach Ltd.
Tel: +447427600266
Twitter: @bwghughes
http://about.me/bwghughes


No problem, glad to hear you're liking peewee!
Issue: set the default charset to utf8 when create_table
Author: wong2
State: closed
Created: 2012-03-30 06:22:30

when create table, set the default charset to utf-8 to support more languages such as Chinese.


Forgot to say that I just tested it in MySQL.


Does this not work? https://github.com/coleifer/peewee/blob/master/peewee.py#L244


I think that is the charset when connecting to MySQL, it doesn't specify the default charset for creating tables so when I insert Chinese words to the table, they are garbled
Issue: Fix problems with models which have a "model" field
Author: karlb
State: closed
Created: 2012-03-23 14:15:49

The same solution is already being used by the Q class.


this is probably an issue in more places than just this


As a workaround you could try using the db_column attribute on your field named "model":

``` python

class MyModel(Model):
    model_alias = CharField(db_column='model')
```


So far my project works fine with the change in the pull request (without workaround). Should I look for more places in the code with this problem and submit another pull request, or is there another plan?


I apologize for taking a while getting to this, I'll see about merging something up in the next day or two.
Issue: Raw Expression select with value
Author: ManinTheVan
State: closed
Created: 2012-03-13 14:16:18

Hello Charles, 

want to use 'select' in the following way. 

  << User.select(['id', 'username', R('LOWER(SUBSTR( %s, 1, 1))', 'myFirstName' , 'first_letter')]) >>

but there is no implementation using values and alias together ? 
Have you any idea how to fix it? 

Kind regards, 

Chris


Yeah, only works like this in where clauses right now.  Will look into it


I'm actually not sure whether this is a valid use-case -- in your example you are querying for the first letter of a constant ("myFirstName") -- what is the benefit of doing that?

I'm assuming the query you're hoping to generate is something like (using sqlite style parameter interpolation):

``` python
>>> query = User.select(['id', 'username', R('LOWER(SUBSTR( %s, 1, 1))', 'myFirstName' , 'first_letter')])
>>> query.sql()
('SELECT "id", LOWER(SUBSTR(?, 1, 1)) AS first_letter FROM "users"', ['myFirstName'])
```

But if you know the "first_letter" is always going to be 'm' why add it to the query?  If you hope to use that alias as part of the where clause you could instead write:

``` python
>>> query = User.select().where(R('LOWER(SUBSTR(username, 1, 1))=%s', 'm'))
>>> query.sql()
('SELECT "id", "username" FROM "users" WHERE LOWER(SUBSTR(username, 1, 1))=?', ['m'])
```

Am I missing something?  I've got this written in a branch but as I was thinking about it I had a hard time thinking of how it would be useful.  Thanks!
Issue: Strange models behavior
Author: gnezim
State: closed
Created: 2012-03-11 09:10:48

Problems started after Commit: 62bd00b90686aea6d6440ff129c30cf28a2a9449 

Whole speak is about MySQL as backend

inside one of models I have method:
<code>
    def getAbbrByCountryId(self, country_id):
        return CC_map.select(
                {CC_map: ['id AS cc_map_id, currency_id'],
                 Currencies: ['currency_abbr']}
            ).order_by('country_id').where(country_id=country_id
            ).join(Currencies, on='currency_id')
</code>

When you provided "smart select_related() behavior" I have no access to attributes created in code above.

Tried latest version and got problems with not assigning table link name to column names.

I get:
<code>SELECT id AS cc_map_id, currency_id, t2.`currency_abbr` 
FROM  `cc_map` AS t1
INNER JOIN  `currencies` AS t2 ON t1.`currency_id` = t2.`id` 
WHERE t1.`country_id` =1
ORDER BY t1.`country_id` ASC </code>

instead of 

<code>SELECT t1.id AS cc_map_id, t1.currency_id, t2.`currency_abbr` 
FROM  `cc_map` AS t1
INNER JOIN  `currencies` AS t2 ON t1.`currency_id` = t2.`id` 
WHERE t1.`country_id` =1
ORDER BY t1.`country_id` ASC </code>

Tell If you need any additional info - I'll provide it.

P.S.
Thanks for your great work!


Glad you like the library!  Here's what I'd suggest you try -- you can pass 2-tuples into select clauses to specify aliases:

``` python

CC_map.select({
    CC_map: [('id', 'cc_map_id'), 'currency_id'],
    Currencies: ['currency_abbr'],
}).order_by('country_id').where(country_id=country_id).join(Currencies, on='currency_id')
```


In 0.9.2 that does the trick. Thanks
Issue: DecimalField ignores precision
Author: emestee
State: closed
Created: 2012-03-06 14:02:22

At least under MySQL, DecimalField ignores the precision parameters passed to the constructor, and the created table always carries the decimal field with precision 10,5. 

Example code:

``` python
class Foo(peewee.Model):
    foo_field = DecimalField(15,2)

Foo.create_table()
```

Produces a table with foo_field type set to decimal(10,5)


You need to set the arguments using keywords max_digits and decimal_places:

```
class Foo(Model):
    foo_field = DecimalField(decimal_places=2)
```

```
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int(11)       | NO   | PRI | NULL    | auto_increment |
| df    | decimal(10,2) | NO   |     | NULL    |                |
+-------+---------------+------+-----+---------+----------------+
```


Apologies, I feel pretty silly for not paying attention to the documentation.

Please do note that the docs says the decimal_places argument is simply _places_ at http://charlesleifer.com/docs/peewee/peewee/fields.html#field-types-table
Issue: In memory SQLite support?
Author: stodge
State: closed
Created: 2012-02-26 02:57:01

I may have missed this in the docs, but does Peewee support in memory SQLite databases? Thanks


My apologies - I got it working. :)


Yeah just set the database name to ":memory:"
On Feb 25, 2012 8:57 PM, "Mike Stoddart" <
reply@reply.github.com>
wrote:

> I may have missed this in the docs, but does Peewee support in memory
> SQLite databases? Thanks
> 
> ---
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/coleifer/peewee/issues/59
Issue: Define database name at runtime?
Author: stodge
State: closed
Created: 2012-02-20 01:45:14

I don't know the name of my database until runtime. Is there any way to support this?

Thanks


the database name isn't needed until a connection is opened, so you _could_ manually set the name at runtime before a query gets executed or a connection opened.  what is your use-case?  its kind of tricky considering that your model definitions are already written, unless you are creating those dynamically as well.


I found a solution. I can create the models and database name dynamically at runtime from json descriptions. Initial testing shows it works. Thanks
Issue: sql meta information generates wrong column info in some cases
Author: coleifer
State: closed
Created: 2012-02-15 01:56:16

```

In [6]: sq.sql_meta()
Out[6]: 
('SELECT t2."feature", SUM(t1."count") AS count FROM "featurecount" AS t1 INNER JOIN "feature" AS t2 ON t1."feature_id" = t2."id" GROUP BY t2."feature"',
 [],
 {'columns': [(models.FeatureCount, ('SUM', 'count', 'count')),
   (models.Feature, 'feature')],
  'graph': {models.FeatureCount: [(models.Feature, None, None)]}})
```

Should be:

```
'columns': [(models.Feature, 'feature'), (models.FeatureCount, ('SUM', 'count', 'count'))]
```

```
```
Issue: Duplicated key
Author: wtld
State: closed
Created: 2012-02-08 16:00:37

Table created in MySQL by create_table method:

``` sql
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `test_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
```

Key on 'id' column is duplicated (PRIMARY KEY and UNIQUE KEY).


Would you mind sharing your model definiton?


``` python
class BaseModel(peewee.Model):

    class Meta:
        database = database

class Test(BaseModel):
    name = peewee.CharField()

Test.create_table()
```
Issue: Cache?
Author: 23doors
State: closed
Created: 2012-02-03 21:19:00

Is peewee caching any results?
I would love to see caching mechanisms here like in https://bitbucket.org/jmoiron/johnny-cache.


If you iterate over a QueryResultWrapper, the object that wraps a db cursor when doing a select queyr, results will be cached in memory (if iterating over a large data set you can use the queryresultwrapper.iterator() which doesn't cache things).

Beyond that there is no caching, nor do i think there should be.  ORM level caching always seemed a little sketchy to me, probably the best place to put caching is higher up, in the application.
Issue: Select for update
Author: coleifer
State: closed
Created: 2012-01-31 15:48:31

FROM @23doors:

There doesn't seem to be any kind of for update functionality? This is really very important for simple row-lock operations.
What I mean is a functionality like this one:
http://docs.sqlalchemy.org/en/latest/core/expression_api.html#sqlalchemy.sql.expression.select
(for_update parameter)
Issue: Pony request, support for something akin to django orm's .extra()
Author: coleifer
State: closed
Created: 2012-01-30 15:36:19

Currently you are limited, more or less, to expressing select queries using the available abstractions - simple field queries, queries against column data (F objects), aggregations (min/max/count/etc), and aliases (foo as bar).  To go beyond that requires using the RawQuery, which exposes a stripped down interface.  Be nice to be able to add arbitrary elements to the select query to express things like "LOWER()" or other functions.  Also be nice to add it to the where() clause to express things like CASE statements and the like.


Woohoo!!  Added!

http://charlesleifer.com/docs/peewee/peewee/querying.html#sql-functions-raw-expressions-and-the-r-object
Issue: Is table name not allowed numerical value?
Author: ghost
State: closed
Created: 2012-01-29 07:11:37

Hi.
I use peewee 0.8.0 and MySQL5.5.14,SQLite3.7.5 on MacOSX Lion.

Is table name not allowed like MyTable1 and My1Table?
Numerical value is replaced '_' automatically.

e.g.
MyTable1.create_table() ---> mytable_
My1Table.create_table() ---> my_table

Is this specifications?

Thank you.
Issue: Complex RawQuery?
Author: davidthewatson
State: closed
Created: 2012-01-19 21:33:55

I'm trying to run a RawQuery that creates a view and involves several tables. Is there a way to do this in peewee? It seemed like RawQuery only accounts for simple cases tied to a single table in the data model. Is there a way to do a raw SQL query independent of the data model?

Thanks!


Yeah, RawQuery is best for querying against a particualr "model" as it willr eturn instances.  To execute arbitrary queries you could look in the code and see what's happening, but basically:

`database.execute("some sql", (any, parameters, here))`
Issue: Unicode value and lookup tests
Author: jokull
State: closed
Created: 2012-01-19 15:14:54

They pass for SQLite but not for PostgreSQL.


What's failing with the postgresql tests?


The return values from psycopg2 are `str` types, non-decoded. Rev 4a2ba5b adds encoding and decoding and the tests pass.

Here's the result before the patch:

# 

## FAIL: test_unicode_lookup (tests.UnicodeFieldTests)

Traceback (most recent call last):
  File "/Users/jokull/Code/peewee/tests.py", line 1104, in test_unicode_lookup
    self.assertEqual(a1.title, a.title)
AssertionError: 'L\xc3\xbd\xc3\xb0veldi\xc3\xb0 \xc3\x8dsland' != u'L\xfd\xf0veldi\xf0 \xcdsland'

# 

## FAIL: test_unicode_value (tests.UnicodeFieldTests)

Traceback (most recent call last):
  File "/Users/jokull/Code/peewee/tests.py", line 1097, in test_unicode_value
    self.assertEqual(a.title, u'Lýðveldið Ísland')
AssertionError: 'L\xc3\xbd\xc3\xb0veldi\xc3\xb0 \xc3\x8dsland' != u'L\xfd\xf0veldi\xf0 \xcdsland'


I'm confused...is this working?


First commit is the test case so u can see what fails. Second commit fixes it.

Sent from my iPhone

On Jan 19, 2012, at 9:43 PM, Charles Leifer
reply@reply.github.com
wrote:

> I'm confused...is this working?
> 
> ---
> 
> Reply to this email directly or view it on GitHub:
> https://github.com/coleifer/peewee/pull/46#issuecomment-3574890


For what it's worth the Django ORM does this too.


Thanks for all your work on this!  Merged into master and verified your tests against psql & sqlite.  Tested against MySQL as well and am getting failures:

```
======================================================================
FAIL: test_unicode_lookup (tests.UnicodeFieldTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/charles/code/peewee/tests.py", line 1012, in test_unicode_lookup
    self.assertEqual(a1.title, a.title)
AssertionError: 'L\xfd\xf0veldi\xf0 \xcdsland' != u'L\xfd\xf0veldi\xf0 \xcdsland'

======================================================================
FAIL: test_unicode_value (tests.UnicodeFieldTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/charles/code/peewee/tests.py", line 1005, in test_unicode_value
    self.assertEqual(a.title, u'Lýðveldið Ísland')
AssertionError: 'L\xfd\xf0veldi\xf0 \xcdsland' != u'L\xfd\xf0veldi\xf0 \xcdsland'
```


Fixed in 0c3b2ef62a83bfdd964be1739817a34d87ea4548
Issue: sqlite syntax error when a field is named group
Author: aflower
State: closed
Created: 2012-01-08 12:03:10

The orm generates this sql string

 CREATE TABLE thethable (stat INTEGER NOT NULL, group VARCHAR(255) NOT NULL) 

for a model that has stat an integerField and group a CharField.

sqlite3 then gives an operationalError: near "group" syntax error 

probably since group is a keyword, but if we put quote marks around group, the statement passes.

a possible fix is to change the line 1803 (Field class, to_sql method) in peewee.py from return '%s %s' % (self.name, rendered) to
return '"%s" %s' % (self.name, rendered)


Thanks for reporting, looking -- the quote thing may not work as I think mysql likes backticks, but this is definitelysomething that needs to be addressed.


Nice fix!


peewee now quotes columns and table names as well...closing
360e55f28a606dfeabc68a5c7dcabb9e109ec0bd
6b2a8278d9a068b71ae110f37626d053bdbcec1d
Issue: Help with alter table queries
Author: coleifer
State: closed
Created: 2012-01-07 19:46:19

Although there are no automated 'migrations' in peewee, it would be helpful to give users a way to get column sql and generate alter table queries (or with sqlite, since there's no drop column, maybe the temptable thing).


Decided that the range of possible alter table queries really precludes a general purpose solution that wouldn't involve a whole lot of code.  Going to leave this up to the end-user to handle if it goes beyond the most basic case (add/drop/rename col).
Issue: Added __isnull column lookup for Django-style NULL and NOT NULL value checking
Author: drewyeaton
State: closed
Created: 2012-01-02 04:41:41

I realize this can be accomplished with a Q object, but it's arguably harder to read queries using them. This also is available in the standard Django API, so it may not hurt to have more feature parity on this level. 

Also, added some tests for "__isnull" and for use in conjunction with Q object (no idea why one would ever do this though).


Thanks mane!  Looks great


No problem!
Issue: Added __isnull column lookup for Django-style NULL and NOT NULL value
Author: drewyeaton
State: closed
Created: 2011-12-31 05:08:38

I realize this can be accomplished with a Q object, but it's arguably harder to read queries using them. This also is available in the standard Django API, so it may not hurt to have more feature parity on this level.


Add some tests and I'll be happy to merge.