Issue: The record is not saving in only one Table
Author: d1n0z
State: closed
Created: 2023-01-14 10:55:57

Only the "c" part is saved, although I do exactly the same thing, the "u" part changes, but it is not saved in the database. When I put .save (force_insert) it gives an error:

`UNIQUE constraint failed: Users.id`

Code(3 files):

```
# main.py
#  self.event.text.lower()[15:] - user id
#  self.db - current user database
c = utils.getClanById(self.db.userId)
c.users = c.users.replace(f"{self.event.text.lower()[15:]}/", "")
c.save()
u = utils.getUserById(int(self.event.text.lower()[15:]))
u.user_clan = ""
u.save()

# utils.py
import time
from models import *
def getUserById(user_id):
    try:
        return User().get(User.userId == user_id)
    except:
        User(
            userId=user_id,
            name="",
            lastTimeUsedBonus=time.time() - 86400,
            money=0,
            GB=0,
            crowns=0,
            place=1000,
            user_clan="",
            clan_invites=""
        ).save()
        return User().get(vk_id=user_id)
def getClanById(user_id, buy=False):
    user_id = int(user_id)
    try:
        return Clan().get(Clan.owner_id == user_id)
    except:
        if buy:
            Clan(
                owner_id=user_id,
                name="",
                idd=0,
                users="",
                base=1,
                rating=0,
                coffers=0,
                wins=0,
                loses=0
            ).save()
            return Clan().get(Clan.owner_id == user_id)

# models.py
from peewee import *

dbu = SqliteDatabase('users and clans.db')


class User(Model):
    class Meta:
        database = dbu
        db_table = 'Users'

    userId = IntegerField()
    name = TextField()
    lastTimeUsedBonus = TimeField()
    money = IntegerField()
    GB = IntegerField()
    crowns = IntegerField()
    user_clan = TextField()
    clan_invites = TextField()


class Clan(Model):
    class Meta:
        database = dbu
        db_table = 'Clans'

    owner_id = IntegerField()
    name = TextField()
    idd = IntegerField()
    users = TextField()
    base = IntegerField()
    rating = IntegerField()
    coffers = IntegerField()
    wins = IntegerField()
    loses = IntegerField()


if __name__ == '__main__':
    dbu.create_tables([Clan, User])
```

>```c.users = c.users.replace(f"{self.event.text.lower()[15:]}/", "")
c.save()```

This is so far off-base I don't really know where to start. But try something like this:

```
User.update(user_clan='').where(user_clan=x).execute()
```

Where x is the old clan name...if I understand this right.

I suggest reading a beginner level sql tutorial, then go through some of the peewee docs like the quick start.

> > 
> 
> c.save()```
> 
> This is so far off-base I don't really know where to start. But try something like this:
> 
> ```
> User.update(user_clan='').where(user_clan=x).execute()
> ```
> 
> Where x is the old clan name...if I understand this right.

Yes, i already did like this, but record is not stored in database even though print() says otherwise

I think you have some sort of basic confusion about how peewee works so I suggest spending time really going through the docs and examples. For questions like this stack overflow is the place to ask.

> I think you have some sort of basic confusion about how peewee works so I suggest spending time really going through the docs and examples. For questions like this stack overflow is the place to ask.


I already read the docs, did everything right (as in the examples, but for myself), but as I understand it doesn’t work like that, because, as I said above, it simply not saving to the database. Am I right?

Now I don't understand anything. `User.insert(...).where(...).execute()` immediately wrote to the database, and `User.update(...).where(...).execute()` refuses to work no matter how I wrote this. So can you give me at least hint?

@coleifer can You tell something pls?

Insert...where is not even valid sql (although peewee will prevent including the where clause). Again I suggest some sql tutorials.

.where(...) was a typo, sorry, in code it was just User.insert(...).execute(). I think I know how sql works, I was learning sql in the past btw