akumoth/peewee-issues huggingface dataset:
Issue: Meta Class fields not inherited
Author: stephenfin
State: closed
Created: 2015-03-08 17:36:41

Take two models that demonstrate inheritance:

```
class BaseSchema(Schema):
    """Base serializer."""
    id = fields.Integer(dump_only=True)
    created_at = fields.DateTime(dump_only=True)

    class Meta:
        ordered = True

class PersonSchema(BaseSchema):
    """Person serializer."""
    updated_at = fields.DateTime(dump_only=True)
    name = fields.String()
    email = fields.Email(required=True)
```

The `PersonSchema` schema inherits all the attributes of the `BaseSchema`, as expected. However, it does not seem to inherit the meta attributes (i.e. `ordered`). To ensure ordered output, it is necessary to include these two lines in every model:

```
class Meta:
    ordered = True
```

I don't know if this is something that could be solved or not (I'm not all that familiar with the implementation of these 'Meta' classes).


This doesn't look like peewee to me.


Nope - it's Marshmallow. My apologies.
Issue: Max length reflection
Author: garar
State: closed
Created: 2015-03-08 00:38:12

Hello!

I've added support for max_length to reflection. Previously reflection didn't read max_length for varchar or char.

Left me know if you need anything else for this.

Thanks!


I think I'm going to pass on this, but thank you for the PR.


Hi!

Can I get some arguments against it? Currently when you run generate_models from Introspector it will return invalid models. max_length with be incorrect for CHAR and VARCHAR fields. I would call it a bug.

Thanks!
Issue: postgresql DEFAULT now()
Author: mmongeon-aa
State: closed
Created: 2015-03-06 20:16:38

I'm trying to create a table in PostgreSQL that uses timestamp with time zone field default now()

```
from playhouse.postgres_ext import *
from peewee import *
from playhouse.csv_loader import load_csv

db = PostgresqlExtDatabase('mydb', host='127.0.0.1', user='user', password='pass')

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

class my_table(BaseModel):
    rpt_grp = TextField()
    rpt_int_grp = TextField(null=True)
    event_name = TextField()
    form_id = TextField()
    start_ts = DateField()
    end_ts = DateField(null=True)
    ts_insert = DateTimeTZField(default='now()')
    ts_update = DateTimeTZField(default='now()')

db.connect() 
my_table.create_table()
```

This creates a table successfully with the following DDL:

```
CREATE TABLE my_table
(
  id serial NOT NULL,
  rpt_grp text NOT NULL,
  rpt_int_grp text,
  event_name text NOT NULL,
  form_id text NOT NULL,
  start_ts date,
  end_ts date,
  ts_insert timestamp with time zone NOT NULL,
  ts_update timestamp with time zone NOT NULL,
  CONSTRAINT my_table_pkey PRIMARY KEY (id)
)
```

What i really need peewee to do is create a table that returns this:

```
CREATE TABLE my_table_now
(
  id serial NOT NULL,
  rpt_grp text NOT NULL,
  rpt_int_grp text,
  event_name text NOT NULL,
  form_id text NOT NULL,
  start_ts date,
  end_ts date,
  ts_insert timestamp with time zone NOT NULL DEFAULT now(),
  ts_update timestamp with time zone NOT NULL DEFAULT now()
)
```


Peewee does not support database defaults out of the box. You can subclass `playhouse.postgres_ext.DateTimeTZField` and override the `__ddl__` method, however:

``` python
class DefaultNowDateTimeTZField(DateTimeTZField):
    def __ddl__(self, column_type):
        ddl = super(DefaultNowDateTimeTZField, self).__ddl__(column_type)
        ddl.append(SQL('DEFAULT now()'))
        return ddl
```


Peewee supports server-side constraints as of a while ago but I apologize as I never followed through updating this ticket. If you are still curious how to do this, you can:

``` python
timestamp = DateTimeField(constraints=[SQL('DEFAULT now()')])
```


@coleifer it doesn't look like it's working with `SqliteDatabase`

```
self = <peewee.SqliteDatabase object at 0x10522fd68>
sql = 'CREATE TABLE "my_table" ("id" INTEGER NOT NULL PRIMARY KEY, "asin" VARCHAR(255) NOT NULL, "cat... "updated_at" DATETIME NOT NULL DEFAULT now(), FOREIGN KEY ("category_id") REFERENCES "category" ("id"))'
params = [], require_commit = True

    def execute_sql(self, sql, params=None, require_commit=True):
        logger.debug((sql, params))
        with self.exception_wrapper:
            cursor = self.get_cursor()
            try:
>               cursor.execute(sql, params or ())
E               peewee.OperationalError: near "(": syntax error

venv/lib/python3.6/site-packages/peewee.py:3758: OperationalError
```

In SQLite the expression is different:

```sql
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
```
Issue: UnsignedIntegerField?
Author: nilp0inter
State: closed
Created: 2015-03-06 00:01:34

Hi,

I'm trying to create a model with an unsigned integer field but I can't find how to do it with peewee.

Is this possible?


http://docs.peewee-orm.com/en/latest/peewee/models.html#creating-a-custom-field


if you got here through google - like me - here is a quick solution which worked for my use case
``
uid    = SmallIntegerField(null=True, constraints=[SQL("UNSIGNED")])
``

:+1:  thanks for sharing @sooslaca 

got this one worked:

```
class UnsignedIntegerField(IntegerField):
    field_type = 'int unsigned'
```

(peewee 3.3.4)

@sooslaca 
it doesn't work as you said.

You can use @binderclip snippet, which works for Peewee 3.x and newer.

> got this one worked:
> 
> ```
> class UnsignedIntegerField(IntegerField):
>     field_type = 'int unsigned'
> ```
> 
> (peewee 3.3.4)

Good luck using that with decimals.
Issue: In PostgresqlExtDatabase, respect the 'autorollback' option
Author: davidmcclure
State: closed
Created: 2015-03-04 00:07:02

First of all, thanks so much for Peewee! I ran across the problem described in #240, which was fixed by the addition of the `autorollback` database option.

I was setting `autorollback=True`, but still hitting the problem, and realized that the implementation of `execute_sql` in `PostgresqlExtDatabase` (which I'm using) doesn't include the check to see if the transaction should be automatically rolled back if the query throws an exception. I just copied in the two lines from the parent `Database` class that do this.


Thanks for this!
Issue: Traverse multiple FKs to the same model with several joins
Author: sangwa
State: closed
Created: 2015-02-27 11:34:54

Consider the following many-to-many relationship:

``` python
from peewee import Model, PrimaryKeyField, ForeignKeyField, CharField

class Entity(Model):
    id = PrimaryKeyField()
    name = CharField(unique=True)

class Relation(Model):
    class Meta:
        indexes = ((('parent', 'child'), True),)
    id = PrimaryKeyField()
    parent = ForeignKeyField(Entity, related_name='children')
    child = ForeignKeyField(Entity, related_name='parents')
    data = CharField()
```

If I want to fetch the list of _relations_ and access their members of **both** sides ("parent" and "child") without additional queries, for example:

``` python
parents = Entity.alias()
children = Entity.alias()
relations = (Relation
                .select(Relation, parents, children)
                .join(parents, on=(Relation.parent == parents.id))
                .switch(Relation)
                .join(children, on=(Relation.child == children.id)))
for rel in relations:
    print rel.id, rel.parent.name, rel.children.name
```

this won't work, because the current implementation of joins will only scan the list of fields up to the **first** foreign key to the joined model, so the second `.join(children, ...)` in the code above will stop scanning on the `parent` attribute and set it second time after the first `.join(parents, ...)` instead of setting the `child` attribute which is actually specified in the expression of the join (and data for it is correctly selected by the generated query).

To overcome this, the patch introduced iterates over all the foreign keys to the same model and selects the appropriate target field based on the join expression, if provided with, so the example code above works properly.


The way to do this with peewee currently is to use aliases in the joins, which tell peewee what attribute to patch the related data onto.

If I modify your example,

``` python
relations = (Relation
                .select(Relation, parents, children)
                .join(parents, on=(Relation.parent == parents.id).alias('parent'))
                .switch(Relation)
                .join(children, on=(Relation.child == children.id).alias('child')))
```

I can get the related values and ensure that they are being retrieve in a single query:

``` python
from playhouse.test_utils import assert_query_count
with assert_query_count(1):
    for rel in relations:
        print rel.id, rel.parent.name, rel.child.name
```


Thanks! Tried aliases before but somehow it didnt work.
Issue: Issue with database.transaction()  for playhouse.flask_utils import FlaskDB 
Author: havannavar
State: closed
Created: 2015-02-26 17:11:23

I am using FlaskDB

```
from flask import Flask
from playhouse.flask_utils import FlaskDB

  DATABASE = {
'name': 'testdb',
'engine': 'playhouse.pool.PooledPostgresqlExtDatabase',
'user': 'sats',
'max_connections': 32,
'stale_timeout': 600,
 }
app = Flask(__name__)
app.config.from_object(__name__)
database = FlaskDB(app)
```

How to use database.transaction()  as mentioned below
# Explicitly roll back a transaction.

```
with database.transaction() as txn:
    do_some_stuff()
    if something_bad_happened():
        # Roll back any changes made within this block.
        txn.rollback()
```

as FlaskDB has no attribute 'transaction'


You need to access the `peewee` database associated with the Flask `Database` object. So,

``` python
with database.database.transaction():
    ...
```
Issue: Usage around insert_many
Author: MartynBliss
State: closed
Created: 2015-02-26 09:17:32

Not really an issue...

It would be helpful if when doing an insert_many there would be a way to get a recommended chunk size given a model. If the max number of sql parameters is fixed, then returning that divided by the number of fields that each row insert would generate?


In order to keep peewee lightweight, I think I will pass on adding this functionality.
Issue: Using load_csv on files generated with dump_csv and using a database model doesn't work
Author: MartynBliss
State: closed
Created: 2015-02-23 12:15:53

When the fields are iterated as part of creating in memory database table, primary keys are being stripped from column names, but the row data is not being modified. So what happens is field(1) gets the value of field(0) and so on


Nice catch, thanks for letting me know.
Issue: MySQL full text search
Author: leebrooks0
State: closed
Created: 2015-02-21 09:57:48

Is there any support for MySQL full text search? I see you can call fn.any_function(), but I am not sure how you would use that when using MATCH AGAINST?


Check out http://docs.peewee-orm.com/en/latest/peewee/querying.html#adding-user-defined-operators


@coleifer Is there a way to add the FULLTEXT index on the model?

Looks like MySQL syntax is `create fulltext index ...`, which is not supported by peewee's out-of-the-box schema manager. Your best bet is to just run it as a one-off query:

```
db = MySQLDatabase(...)

def create_schema():
    db.create_tables(list_of_models)
    db.execute_sql('CREATE FULLTEXT INDEX ...')
```

@coleifer the above link is expired. I can't see the way to implement full text search in mysql

http://docs.peewee-orm.com/en/latest/peewee/query_operators.html#adding-user-defined-operators
Issue: [Feature] Ability to explicitly switch db connection per query 
Author: alexlatchford
State: closed
Created: 2015-02-19 23:47:16

Currently models are defined globally and you have to specific the database in the metaclass, I'd like a way to switch db connection explicitly. I understand that the execution_context decorator exists but still only worked with a single peewee connection pool (i.e. threads).

For my use case I've created a manager class to creates new connection pools per process to get around psycopg2's limitations in that respect. This works great but means I have to hook into private member variables to override the database attribute on Query objects to specify the correct connection (or it'll use the parent/global one that I specified when defining the module).

I'm thinking of a syntax something like this:

``` python
from peewee import *
from multiprocessing import Process

global_db_conn = PostgresqlDatabase(...)
class MyModel(Model):
    name = CharField()

    class Meta:
        database = global_db_conn

MyModel.create_table()

def run():
    process_db_conn = PostgresqlDatabase(...)
    my_process_obj = MyModel.db(process_db_conn).get()  # Or select/update/delete etc.

my_proc = Process(target=run)
my_proc.start()
my_proc.join()
```

Not sure if I've explained the problem or covered all the bases or whether there is a nicer solution but this is certainly a problem I'd love to upvote! (If I get a vote that is)

Thanks,
Alex


PS. My current solution is something like:

``` python
query = MyModel.select()
query.database = process_db_conn
result = query.execute()
```

Works just about, having some teething problems with connections hanging around too though but think it's psycopg2 rather than peewee :)


Thanks for the feature request, seems like a good one. There's a little bit of prior art in the `playhouse.test_utils` module for using a separate database in a context manager for testing. 


Added a new `Using` context manager to the `playhouse.read_slave` module. Still needs docs, but you can find examples in the tests.


Awesome! Cheers Charles, I've put in a ticket into our system to move over to using this I'll let you know once we're using it in LIVE :)


Yikes, good luck!
Issue: Support for pysqlcipher3
Author: tfeldmann
State: closed
Created: 2015-02-17 09:20:50

The sqlcipher_ext in playhouse uses pysqlcipher, which is only compatible with Python 2.7.x.
David Riggleman recently forked this module to work with both Python 2 + 3: https://github.com/rigglemania/pysqlcipher3

It would be great if peewee used this module to be able to work with encrypted databases in Python 3.
Issue: Keyword URLs supported in database connection?
Author: SamuelMarks
State: closed
Created: 2015-02-15 01:12:41

Looking through your examples, looks like I need seperated values rather than a single URL.

So going off that:

```
from os import environ
from operator import add

environ.setdefault('HEROKU_POSTGRESQL_AMBER', 'postgres://username:password@hostname:PORT/dbname')

username, password, hostname, PORT, dbname = reduce(
    add, reduce(add,
                map(lambda e: map(lambda i: i.split('/'), e),
                    map(lambda elem: elem.split(':'),
                        (lambda e: e[e.find('//') + 2:].split('@'))(
                            environ['HEROKU_POSTGRESQL_AMBER']
                        )))))
```

I can then do:

```
from playhouse.postgres_ext import PostgresqlExtDatabase

psql_db = PostgresqlExtDatabase(dbname, user=username)
```

Unfortunately `PostgresqlExtDatabase.__init__` isn't documented; and just uses `*args` and `**kwargs` everywhere.

**How do I provide all these details to the initialiser?**


http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#db-url
On Feb 14, 2015 5:12 PM, "Samuel Marks" notifications@github.com wrote:

> Looking through your examples, looks like I need seperated values rather
> than a single URL.
> 
> So going off that:
> 
> from os import environ
> from operator import add
> 
> environ.setdefault('HEROKU_POSTGRESQL_AMBER', 'postgres://username:password@hostname:PORT/dbname')
> 
> username, password, hostname, PORT, dbname = reduce(add,
>                                                     reduce(add,
>                                    map(lambda e: map(lambda i: i.split('/'), e),
>                                        map(lambda elem: elem.split(':'),
>                                            environ['HEROKU_POSTGRESQL_AMBER'][
>                                            environ['HEROKU_POSTGRESQL_AMBER'].find(
>                                                '//') + 2:].split('@')))))
> 
> I can then do:
> 
> from playhouse.postgres_ext import PostgresqlExtDatabase
> 
> psql_db = PostgresqlExtDatabase(dbname, user=username)
> 
> Unfortunately PostgresqlExtDatabase.**init** isn't documents and just
> uses _args and *_kwargs everywhere.
> 
> _How do I provide all these details to the initialiser?_
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/coleifer/peewee/issues/528.


You can use the db_url module or you can check the documentation on psycopg2. Besides the name of the database, all the other args are passed directly to psycopg2.


Thanks, that worked.
Issue: Cannot create tables with circular foreign key dependencies with SQLite
Author: bfontaine
State: closed
Created: 2015-02-13 21:19:24

TL;DR: SQLite doesn’t support the `ALTER TABLE … ADD CONSTRAINT` syntax used by `db.create_foreign_key`.

---

Hello,

I’m trying to store genealogical data in an SQLite database with the following simplified models:

``` python
FamilyProxy = Proxy()

class Person(Model):
    # the family where this person is a child in
    family = ForeignKeyField(FamilyProxy, related_name='children')

class Family(Model):
    father = ForeignKeyField(Person, related_name='father_in')
    mother = ForeignKeyField(Person, related_name='mother_in')

FamilyProxy.initialize(Family)
```

The [doc about circular dependencies](http://peewee.readthedocs.org/en/latest/peewee/models.html#circular-foreign-key-dependencies) mentions the `Proxy` workaround and at the end shows an example of what we need to do when creating tables, which gives this for my example:

``` py
db.create_tables([Person, Family], safe=True)
db.create_foreign_key(Person, Person.family)
```

And here lies the problem: the `create_foreign_key` method called here gives a syntax error with SQLite:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mymodule/store.py", line 68, in create_tables
    db.create_foreign_key(Person, Person.family)
  File "/…/lib/python2.7/site-packages/peewee.py", line 2974, in create_foreign_key
    model_class, field, constraint))
  File "/…/lib/python2.7/site-packages/peewee.py", line 2877, in execute_sql
    self.commit()
  File "/…/lib/python2.7/site-packages/peewee.py", line 2732, in __exit__
    reraise(new_type, new_type(*exc_value.args), traceback)
  File "/…/lib/python2.7/site-packages/peewee.py", line 2869, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: near "CONSTRAINT": syntax error
```

I added a `print` statement in the code to understand the syntax error:

``` sql
ALTER TABLE "person" ADD CONSTRAINT "fk_person_family_id_refs_family" FOREIGN KEY ("family_id") REFERENCES "family" ("id")
```

SQLite doesn’t support the `ALTER TABLE … ADD CONSTRAINT` syntax ([source](http://www.sqlite.org/omitted.html)), which is why there’s a syntax error here.

---

I can rewrite my code to avoid circular dependencies (with more complex queries), but you might want to add a notice in the documentation about this issue with SQLite.


> SQLite doesn’t support the ALTER TABLE … ADD CONSTRAINT syntax (source), which is why there’s a syntax error here.

Good point. I think I will leave this out as anyone using SQLite probably should already be aware of the limited support for alter table. And circular FKs are a bad design!
Issue: Are mysql migrations still not tested?
Author: leebrooks0
State: closed
Created: 2015-02-10 15:36:23

In the docs it says that migrations have not been well tested with mysql? Is this still correct?


No, they are tested now. Thanks!


Smashing, looking forward to using your ORM!

On Tue, Feb 10, 2015 at 5:39 PM, Charles Leifer notifications@github.com
wrote:

> No, they are tested now. Thanks!
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/coleifer/peewee/issues/524#issuecomment-73720653.
Issue: Inserting record into link table throws OperationalError
Author: rotsj
State: closed
Created: 2015-02-04 12:57:52

Am trying to insert a record into a link table but getting an OperationalError thrown. I believe my model definitions are correct, but any feedback is welcome.

Output:

```
Traceback (most recent call last):
  File "permDb.py", line 129, in <module>
    main()
  File "permDb.py", line 22, in main
    setup()
  File "permDb.py", line 85, in setup
    convertConfig()
  File "permDb.py", line 118, in convertConfig
    project_team.save()
  File "/home/xrogder/scripts/peewee.py", line 3887, in save
    rows = self.update(**field_dict).where(self._pk_expr()).execute()
  File "/home/xrogder/scripts/peewee.py", line 2614, in execute
    return self.database.rows_affected(self._execute())
  File "/home/xrogder/scripts/peewee.py", line 2243, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/home/xrogder/scripts/peewee.py", line 2877, in execute_sql
    self.commit()
  File "/home/xrogder/scripts/peewee.py", line 2732, in __exit__
    reraise(new_type, new_type(*exc_value.args), traceback)
  File "/home/xrogder/scripts/peewee.py", line 2869, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: near "WHERE": syntax error
```

My models are:

``` python
database = peewee.SqliteDatabase("wee.db")

class Project(peewee.Model):
    name = peewee.CharField(unique=True)
    class Meta:
        database = database

class Team(peewee.Model):
    name = peewee.CharField(unique=True)
    class Meta:
        database = database

class Project_Team(peewee.Model):
    project = peewee.ForeignKeyField(Project)
    team = peewee.ForeignKeyField(Team)
    class Meta:
        database = database
        primary_key = peewee.CompositeKey('project', 'team')
```

The code that's executing the insert:

``` python
for project in Project.select():
    new_team = Team()
    new_team.name = team
    try:
        new_team.save()
        project_team = Project_Team()
        #print "project id ", project.id
        #print "team id ", new_team.id
        project_team.project = project.id
        project_team.team = new_team.id
        project_team.save()
    except peewee.IntegrityError:
        # Team already exists
        pass
```

The commented out print statements have shown me that both values exist. In this case, we've just created the first record in the Team table and are now trying to link it to the first project. (1,1)


The problem is the call to `project_team.save()`. Because this model has a non-auto-incrementing primary key, if you are `INSERT`-ing a row you need to call `save(force_insert=True)`.

http://docs.peewee-orm.com/en/latest/peewee/models.html#id3


Works like a charm, but I guess you'd expect that. Thanks!