Search code examples
pythonmysqlpeewee

How to fix "peewee.InternalError: (1364, "Field 'id' doesn't have a default value")" when data is inserted


I am inserting some data into Heroku MySQL database via peewee

Place.create(day="1", time="20", place="1")

and I get error

peewee.InternalError: (1364, "Field 'id' doesn't have a default value")

I have found an answer which said that i need to turn off strict mode in MySQL, but it seems like it didn't work. I have also tried to add auto_increment to id, but looked pointless since it already had primary_key value

This is a booking app for a bar, i want to connect User model and Place model with Reservation model.

class User(BaseModel):
    id = IntegerField(primary_key=True)
    username = CharField(unique=True)

class Place(BaseModel):
    id = IntegerField(primary_key=True)
    day = CharField()
    time = CharField()
    pl = CharField()

class Reservation(BaseModel):
    id = IntegerField(primary_key=True)
    u_id = ForeignKeyField(User, backref='reservs')
    p_id = ForeignKeyField(Place, backref='reservs')

And this piece of code loads data into Place. The last line gives me error.

for day in range(1,8):
    for time in range(18, 26, 2):
        for p in range(1, 9):
            Place.create(day=str(day), time=str(time), pl=str(p))

I have successfully inserted this into SQLite database locally, but it doesn't work with MySQL at Heroku at all


Solution

  • You should use AutoField instead of IntegerField(primary_key=True). AutoField should be used when you want auto-incrementing integer primary key. IntegerField(primary_key=True) implies that you are explicitly specifying the integer value for the primary key, which is rarely what you actually want.