Issue: ManyToManyField add multiple (list, tuple) fails on python 2.7
Author: kpoman
State: closed
Created: 2018-06-08 14:18:14

Hello,
I am testing my following code using ManyToManyField trying to add multiple relations. It works perfectly on Python 3.6, but fails on 2.7. Here is the error:

The code:
```
import peewee

db = peewee.SqliteDatabase('testdb.sqlite', check_same_thread=False)

SPECIALTY_CHOICES = (
    (0, 'Sur mesure'),
    (1, 'Reparations'),
    (2, 'Vente grandes marques'),
    (3, 'Mise au gout bijoux existants'),
    (4, 'Collections propres')
)

STYLE_CHOICES = (
    (0, 'Romantique'),
    (1, 'Contemporain'),
    (2, 'Glam rock'),
    (3, 'Art deco'),
    (4, 'Classique')
)

class BaseModel(peewee.Model):
    class Meta:
        database = db

class Styles(BaseModel):
    label = peewee.CharField(32, unique=True)#IntegerField(choices=STYLE_CHOICES)
    
class Specialties(BaseModel):
    label = peewee.CharField(32, unique=True)#IntegerField(choices=SPECIALTY_CHOICES)

class Jeweler(BaseModel):
    email = peewee.TextField(null=True)
    name = peewee.TextField(null=True)
    surname = peewee.TextField(null=True)
    phone = peewee.TextField(null=True)
    raison_sociale = peewee.TextField(null=True)
    num_immatriculation = peewee.TextField(null=True)
    adresse_siege = peewee.TextField(null=True)
    nom_commercial = peewee.TextField(null=True)
    styles = peewee.ManyToManyField(Styles, backref='jewelers')
    specialties = peewee.ManyToManyField(Specialties, backref='jewelers')

class SalePoint(BaseModel):
    name = peewee.TextField()
    address = peewee.TextField()
    email = peewee.TextField()
    jeweler = peewee.ForeignKeyField(Jeweler, backref='salepoints')

def populate_table(table, values):
    try:
        for v in values:
            q = table.insert(label=v[1])
            q.execute()
    except Exception as e:
        print(e)
        
print('create tables')
db.create_tables([Jeweler,
                  SalePoint,
                  Styles,
                  Specialties,
                  Jeweler.styles.get_through_model(),
                  Jeweler.specialties.get_through_model()])
print('done')

print('populate styles ...')
populate_table(Styles, STYLE_CHOICES)

print('populate specialties ...')
populate_table(Specialties, SPECIALTY_CHOICES)
#q = Styles.insert_many(STYLE_CHOICES)#, fields=[Styles.id, Styles.style])

print('create')
j = Jeweler()
print('save')
j.save()
print('add')
j.styles.add(1)
j.styles.add((2, 3, ))
#j.styles.add(3)
```

C:\Python27\python.exe "C:\Program Files\JetBrains\PyCharm 2018.1.3\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 56397 --file C:/tmp/test_peewee/test.py
pydev debugger: process 11320 is connecting

> Connected to pydev debugger (build 181.5087.37)
> create tables
> done
> populate styles ...
> column label is not unique
> populate specialties ...
> column label is not unique
> create
> save
> add
> Traceback (most recent call last):
>   File "C:\Program Files\JetBrains\PyCharm 2018.1.3\helpers\pydev\pydevd.py", line 1664, in <module>
>     main()
>   File "C:\Program Files\JetBrains\PyCharm 2018.1.3\helpers\pydev\pydevd.py", line 1658, in main
>     globals = debugger.run(setup['file'], None, None, is_module)
>   File "C:\Program Files\JetBrains\PyCharm 2018.1.3\helpers\pydev\pydevd.py", line 1068, in run
>     pydev_imports.execfile(file, globals, locals)  # execute the script
>   File "C:/tmp/test_peewee/test.py", line 79, in <module>
>     j.styles.add((2, 3, ))
>   File "C:\Python27\lib\site-packages\peewee.py", line 6089, in add
>     accessor.through_model.insert_many(inserts).execute()
>   File "C:\Python27\lib\site-packages\peewee.py", line 1525, in inner
>     return method(self, database, *args, **kwargs)
>   File "C:\Python27\lib\site-packages\peewee.py", line 1596, in execute
>     return self._execute(database)
>   File "C:\Python27\lib\site-packages\peewee.py", line 2239, in _execute
>     return super(Insert, self)._execute(database)
>   File "C:\Python27\lib\site-packages\peewee.py", line 2014, in _execute
>     cursor = database.execute(self)
>   File "C:\Python27\lib\site-packages\peewee.py", line 2589, in execute
>     return self.execute_sql(sql, params, commit=commit)
>   File "C:\Python27\lib\site-packages\peewee.py", line 2583, in execute_sql
>     self.commit()
>   File "C:\Python27\lib\site-packages\peewee.py", line 2376, in __exit__
>     reraise(new_type, new_type(*exc_args), traceback)
>   File "C:\Python27\lib\site-packages\peewee.py", line 2576, in execute_sql
>     cursor.execute(sql, params or ())
> peewee.OperationalError: near ",": syntax error
> 
> Process finished with exit code -1073741819 (0xC0000005)
> 

I am getting this error on Python 2.7 peewee 3.5

It ran successfully for me using Python 2.7 and Peewee 3.5.

My hunch is that your Python 2.7 is linking against an old version of sqlite that may not support bulk inserts. You can verify by:

```python
import sqlite3
print(sqlite3.sqlite_version)
```

Bulk insert was added in sqlite 3.7.11. See the note in the docs:

http://docs.peewee-orm.com/en/latest/peewee/querying.html#bulk-inserts