Issue: Default value with SQL and returns of Model.create
Author: fy0
State: closed
Created: 2018-03-31 19:29:31

I know peewee not supported SQL default yet, but use `constraints` to instead of is ok, for example:
```python
timestamp = DateTimeField(constraints=[SQL('DEFAULT CURRENT_TIMESTAMP')])
```

It works, but not perfect.

```python
from peewee import *

db = SqliteDatabase(":memory:")


class Test(Model):
    timestamp = DateTimeField(constraints=[SQL('DEFAULT CURRENT_TIMESTAMP')])
    
    class Meta:
        db_table = 'test'
        database = db

db.create_tables([Test], safe=True)

for i in range(5):
    item = Test.create()
    print("%r %r" % (item.id, item.timestamp))

get_ts = lambda x: x.timestamp
print(list(map(get_ts, Test.select().execute())))
```

Results:
```
1 None
2 None
3 None
4 None
5 None
[datetime.datetime(2018, 3, 31, 18, 55, 4), datetime.datetime(2018, 3, 31, 18, 55, 4), datetime.datetime(2018, 3, 31, 18, 55, 4), datetime.datetime(2018, 3, 31, 18, 55, 4), datetime.datetime(2018, 3, 31, 18, 55, 4)]
```

I thought the value of Test.timestamp is from default (None), not from db.
Is there a solution?

BTW: Actually I'm a postgres user, so i can use `Model.insert` to replace `Model.create`, and I found a bug here.

Document said: To select all columns from a particular table, you can simply pass in the Model class.

http://docs.peewee-orm.com/en/latest/peewee/querying.html?highlight=returning#returning-clause

But I got [] after pass Model class:
```python
ret = User.insert(**vals).returning(User).execute()
```
Get right results after pass all fields:
```python
User.insert(**vals).returning(*User._meta.fields.values()).execute()
```

My peewee version is 3.2.0


> I know peewee not supported SQL default yet, but use constraints to instead of is ok

This is the correct way to implement server-side column defaults in Peewee.

Postgres supports a `RETURNING` clause, so you can retrieve the server-created timestamp by specifying the timestamp column, as you have done. With SQLite and MySQL there is no option but to re-fetch the columns with a `SELECT` query.

I've corrected the issue with passing the `Model` class in e6012b21daf9656076b46a7db5a6229d98e0f734 so if you pull the latest version you should be able to pass `Model` classes to returning.

@coleifer Wait, if we can't get results from SQLite and MySQL after inserted, how does a self-increasing primary key works? We don't even know the exact value.

My real case:
```python
class User(BaseModel, BaseUser):
    id = BlobField(primary_key=True, constraints=[SQL("DEFAULT int2bytea(nextval('id_gen_seq'))")])
```


@coleifer

Here's a new issue, looks like a bug about sequence:
db: PostgreSQL 9.6

```python
db.execute_sql('''CREATE SEQUENCE IF NOT EXISTS user_count_seq NO MINVALUE NO MAXVALUE START 1 CACHE 1000 NO CYCLE;''')

class User2(Model):
    number = IntegerField(sequence='user_count_seq') 

User2.create_table(safe=True)
```

Crashed:
```
Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\lib\site-packages\peewee.py", line 2517, in execute_sql
    cursor.execute(sql, params or ())
psycopg2.ProgrammingError: syntax error at or near "NULL"
LINE 1: NULL
```

Let's look at SQL:
```python
print(''.join(User2._schema._create_table()._sql))
```

```SQL
CREATE TABLE IF NOT EXISTS "user2" ("id" SERIAL NOT NULL PRIMARY KEY, "number" INTEGER NOT NULL DEFAULT NEXTVAL('user_count_seq'))
```

Emmmm, SQL is right, so what's up?


Append:
![image](https://user-images.githubusercontent.com/1579850/38172313-dae04ecc-35dc-11e8-9ba1-7a004366923e.png)

![image](https://user-images.githubusercontent.com/1579850/38172316-e8b319c6-35dc-11e8-90a2-8db99b23a993.png)

`params` is [None]

Temporary solution:

```python
def get_user_count_seq():
    return db.execute_sql("select nextval('user_count_seq')").fetchone()[0]


class User2(BaseModel):
    number = peewee.IntegerField(default=get_user_count_seq)


db.create_tables([User2])
```


Sqlite has an API last_rowid() which returns the id of the last-inserted row. MySQL has a similar API. Postgres doesn't need one because it supports returning arbitrary values from the inserted row.

Let me take a look into the sql error.

Let me take a look into the sql error.

Fixed, pushed new release 3.2.2.

Good job!