Issue: ManyToMany-Field creation throws an IntegrityError "Duplicate entry for key"
Author: greece57
State: closed
Created: 2017-10-14 17:57:49

I don't know where that error is coming from, thats why I want to ask here.

I'm doing the following:

```

class Article(BaseModel):

    time = pw.DateTimeField()
    # ...
    hashtags = ManyToManyField(Hashtag, related_name='articles')
    topics = ManyToManyField(Topic, related_name='topics')
    text = pw.TextField()

class Hashtag(BaseModel):
    tag = pw.CharField()
```

And then during execution:

```
article = Article(time=published_utc_datetime, ...) # I define every thing in here which is not a ManyToMany Field

hashtags = get_hashtag() # hashtags is an array of already saved hashtag-peewee-Model-Objects
article.save()
article.hashtags.add(hashtags) # <- Here I get the Error
article.save()
```

This is the Error:
`peewee.IntegrityError, (1062, "Duplicate entry '7-91' for key 'article_hashtag_through_article_id_hashtag_id'")`

The thing is that when I look into the table there is no entry for an Entry with article_id 7 and hashtag_id 91. Its actually the first entry in this table for article_id 7.
```
MariaDB [database]> SELECT * FROM article_hashtag_through WHERE article_id = 7;
Empty set (0.00 sec)
```


This is the database description:
```
MariaDB [database]> describe article_hashtag_through;
+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| id         | int(11) | NO   | PRI | NULL    | auto_increment |
| article_id | int(11) | NO   | MUL | NULL    |                |
| hashtag_id | int(11) | NO   | MUL | NULL    |                |
+------------+---------+------+-----+---------+----------------+

```

I hope you can help me with this because I really don't understand the problem.

Can't replicate:

```python
from peewee import *
from playhouse.fields import ManyToManyField

database = SqliteDatabase(':memory:')

class BaseModel(Model):
    class Meta:
        database = database

class Hashtag(BaseModel):
    tag = TextField()

class Article(BaseModel):
    title = TextField()
    hashtags = ManyToManyField(Hashtag, related_name='articles')

ThroughModel = Article.hashtags.get_through_model()

database.create_tables([
    Hashtag,
    Article,
    ThroughModel])

article = Article(title='testing')
article.save()

ht1 = Hashtag.create(tag='ht1')
ht2 = Hashtag.create(tag='ht2')
article.hashtags.add([ht1, ht2])
article.save()

article_db = Article.get(Article.title == 'testing')
for ht in article_db.hashtags:
    print ht.tag
```

The above code prints "ht1" and "ht2" as expected.

I can't replicate it either on purpose :/
I'm parsing a lot of data and suddenly it happens again and again..
But only on that element.

I posted it here because the Error Message seams to be incorrect. Do you maybe have an idea how I can find out more about it or what I should do next to find the problem?

Please see this line:

```python
hashtags = get_hashtag() # hashtags is an array of already saved hashtag-peewee-Model-Objects
```

I think your get_hashtag function have some problem, so the hashtags have duplicating-objects.

I reproduced it by this script:

```python
hashtags = list(HashTag.select())
hashtags = hashtags.extend(hashtags)
```

A easy way to test, you can try:

```python
hashtags = list(set(get_hashtag())) # hashtags is an array of already saved hashtag-peewee-Model-Objects
```

@yanghg-basefx thank you! Most likely that was the problem and now it seems to be working fine :+1: 

I didn't think of interpreting the error message that way!