Issue: Peewee throw exception on 2nd time select query
Author: Maheshkumar094
State: closed
Created: 2018-09-12 14:00:10

```
class Usr(BaseModel):
    created_at = IntegerField(null=True)
    created_at1 = DateTimeField(null=True)
    created_by = IntegerField(null=True)
    created_by1 = IntegerField(null=True)
    email = CharField()
    is_email_verified = BooleanField(null=True)
    is_test = BooleanField(null=True)
    last_modified_at = DateTimeField(null=True)
    last_modified_by = IntegerField(null=True)
    mobile = CharField()
    name = CharField()
    password = CharField()
    username = CharField()


    class Meta:
        table_name = 'usr'
        schema = 'public'
```


```
class Passenger(BaseModel):
    created_at = DateTimeField(null=True)
    created_by = IntegerField(null=True)
    flexi_time_window = IntegerField(null=True)
    home_address = CharField(null=True)
    home_fav_time = DateTimeField(null=True)
    home_loc = UnknownField(null=True)  # USER-DEFINED
    is_time_flexible = BooleanField(null=True)
    last_modified_at = DateTimeField(null=True)
    last_modified_by = IntegerField(null=True)
    name = CharField()
    non_refundable_credit = DecimalField(null=True)
    office_address = CharField(null=True)
    office_fav_time = DateTimeField(null=True)
    office_loc = UnknownField(null=True)  # USER-DEFINED
    refundable_credit = DecimalField(null=True)
    total_credit_balance = DecimalField(null=True)
    #user = ForeignKeyField(column_name='user_id', field='id', model=Usr)
    user = ForeignKeyField(column_name='user_id', field='id', model=Usr)

    class Meta:
        table_name = 'passenger'
        schema = 'public'


```



```
def is_usr_valid2(email):
    try:
        #sub = model.Usr.get(model.Usr.email == email)
        sub = model.Usr.select().where(model.Usr.email == email)
        record = model.Passenger.select().where((model.Passenger.user ==sub))
        print 'sql ={}'.format(record.sql())
        print  list(record.dicts())
        print "===========User Login Detail ============"
        print  list(record.dicts())[0]['id']
        #print ("id = {} Name ={} \n Email = {} ".format(record.id,record.name, record.email))
        cnf.statuscode = cnf.ok
        cnf.is_error = False
        return record
    except Exception as e:
        print("Error During fetching user detail \n {}".format(e))
        cnf.is_error = True
        cnf.statuscode = cnf.Internal_server_Error
        tb.print_exc()
        #raise
        return None

if __name__ == '__main__':
    #================== first call =============
    print  is_usr_valid2("example@gmail.com")
    #=================== second call ===========
    print  is_usr_valid2("example@gmail.com")
```

>  Above code work fine for time for the second call it got an exception
> on first call it form correct query

 
**('SELECT "t1"."id", "t1"."created_at", "t1"."created_by", "t1"."flexi_time_window", "t1"."home_address", "t1"."home_fav_time", "t1"."is_time_flexible", "t1"."last_modified_at", "t1"."last_modified_by", "t1"."name", "t1"."non_refundable_credit", "t1"."office_address", "t1"."office_fav_time", "t1"."refundable_credit", "t1"."total_credit_balance", "t1"."user_id" FROM "public"."passenger" AS "t1" WHERE ("t1"."user_id" = (SELECT "t2"."id" FROM "public"."usr" AS "t2" WHERE ("t2"."email" = %s)))', [u'example@gmail.com'])**

**Query Result** 

[{'home_fav_time': None, 'home_address': None, 'name': u'Antar ', 'is_time_flexible': None, 'office_address': None, 'created_at': datetime.datetime(2018, 6, 8, 10, 23, 33, 895096, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 'refundable_credit': Decimal('52.000000'), 'created_by': None, 'office_fav_time': None, 'flexi_time_window': None, 'last_modified_at': datetime.datetime(2018, 6, 8, 10, 23, 33, 895096, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 'non_refundable_credit': Decimal('0.000000'), 'total_credit_balance': Decimal('52.000000'), 'last_modified_by': None, 'id': 61, 'user': 129}]

on second call it generates a query

**('SELECT "t1"."id", "t1"."created_at", "t1"."created_by", "t1"."flexi_time_window", "t1"."home_address", "t1"."home_fav_time", "t1"."is_time_flexible", "t1"."last_modified_at", "t1"."last_modified_by", "t1"."name", "t1"."non_refundable_credit", "t1"."office_address", "t1"."office_fav_time", "t1"."refundable_credit", "t1"."total_credit_balance", "t1"."user_id" FROM "public"."passenger" AS "t1" WHERE %s', [False])**

**I can't understand how this where condition generated  "WHERE %s', [False]" come**

@coleifer  I hope Now the code look bit formatted

PLEASE FORMAT YOUR CODE.

Coleifer it is formated in my code base I copy past some part it that why its look unformatted. Ok I will comment code in more formatted way.

FUCK MAN, CLICK "Styling with Markdown is supported" -- it will show you how to highlight your code. There's also a handy "<>" toolbar button on the comment editor.

```
class Usr(BaseModel):
    created_at = IntegerField(null=True)
    created_at1 = DateTimeField(null=True)
    created_by = IntegerField(null=True)
    created_by1 = IntegerField(null=True)
    email = CharField()
    is_email_verified = BooleanField(null=True)
    is_test = BooleanField(null=True)
    last_modified_at = DateTimeField(null=True)
    last_modified_by = IntegerField(null=True)
    mobile = CharField()
    name = CharField()
    password = CharField()
    username = CharField()


    class Meta:
        table_name = 'usr'
        schema = 'public'
```


```
class Passenger(BaseModel):
    created_at = DateTimeField(null=True)
    created_by = IntegerField(null=True)
    flexi_time_window = IntegerField(null=True)
    home_address = CharField(null=True)
    home_fav_time = DateTimeField(null=True)
    home_loc = UnknownField(null=True)  # USER-DEFINED
    is_time_flexible = BooleanField(null=True)
    last_modified_at = DateTimeField(null=True)
    last_modified_by = IntegerField(null=True)
    name = CharField()
    non_refundable_credit = DecimalField(null=True)
    office_address = CharField(null=True)
    office_fav_time = DateTimeField(null=True)
    office_loc = UnknownField(null=True)  # USER-DEFINED
    refundable_credit = DecimalField(null=True)
    total_credit_balance = DecimalField(null=True)
    #user = ForeignKeyField(column_name='user_id', field='id', model=Usr)
    user = ForeignKeyField(column_name='user_id', field='id', model=Usr)

    class Meta:
        table_name = 'passenger'
        schema = 'public'


```



```
def is_usr_valid2(email):
    try:
        #sub = model.Usr.get(model.Usr.email == email)
        sub = model.Usr.select().where(model.Usr.email == email)
        record = model.Passenger.select().where((model.Passenger.user ==sub))
        print 'sql ={}'.format(record.sql())
        print  list(record.dicts())
        print "===========User Login Detail ============"
        print  list(record.dicts())[0]['id']
        #print ("id = {} Name ={} \n Email = {} ".format(record.id,record.name, record.email))
        cnf.statuscode = cnf.ok
        cnf.is_error = False
        return record
    except Exception as e:
        print("Error During fetching user detail \n {}".format(e))
        cnf.is_error = True
        cnf.statuscode = cnf.Internal_server_Error
        tb.print_exc()
        #raise
        return None

if __name__ == '__main__':
    #================== first call =============
    print  is_usr_valid2("example@gmail.com")
    #=================== second call ===========
    print  is_usr_valid2("example@gmail.com")
```

>  Above code work fine for time for the second call it got an exception
> on first call it form correct query

 
**('SELECT "t1"."id", "t1"."created_at", "t1"."created_by", "t1"."flexi_time_window", "t1"."home_address", "t1"."home_fav_time", "t1"."is_time_flexible", "t1"."last_modified_at", "t1"."last_modified_by", "t1"."name", "t1"."non_refundable_credit", "t1"."office_address", "t1"."office_fav_time", "t1"."refundable_credit", "t1"."total_credit_balance", "t1"."user_id" FROM "public"."passenger" AS "t1" WHERE ("t1"."user_id" = (SELECT "t2"."id" FROM "public"."usr" AS "t2" WHERE ("t2"."email" = %s)))', [u'example@gmail.com'])**

**Query Result** 

[{'home_fav_time': None, 'home_address': None, 'name': u'Antar ', 'is_time_flexible': None, 'office_address': None, 'created_at': datetime.datetime(2018, 6, 8, 10, 23, 33, 895096, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 'refundable_credit': Decimal('52.000000'), 'created_by': None, 'office_fav_time': None, 'flexi_time_window': None, 'last_modified_at': datetime.datetime(2018, 6, 8, 10, 23, 33, 895096, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 'non_refundable_credit': Decimal('0.000000'), 'total_credit_balance': Decimal('52.000000'), 'last_modified_by': None, 'id': 61, 'user': 129}]

on second call it generates a query

**('SELECT "t1"."id", "t1"."created_at", "t1"."created_by", "t1"."flexi_time_window", "t1"."home_address", "t1"."home_fav_time", "t1"."is_time_flexible", "t1"."last_modified_at", "t1"."last_modified_by", "t1"."name", "t1"."non_refundable_credit", "t1"."office_address", "t1"."office_fav_time", "t1"."refundable_credit", "t1"."total_credit_balance", "t1"."user_id" FROM "public"."passenger" AS "t1" WHERE %s', [False])**

**I can't understand how this where condition generated  "WHERE %s', [False]" come**

@coleifer  I hope Now the code look bit formatted

My guess is you're overwriting models.Passenger.user somewhere.