Calificación:
  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
peewee.IntegrityError: NOT NULL constraint failed: person.name
#1
Hola, estoy aprendiendo Python en la plataforma Udemy y el siguiente codigo me da errores y aparte no crea la carpeta Pet, agradezco tu ayuda.

from datetime import date
from peewee import *
from peewee import Model

db=SqliteDatabase("people.db")

class Person(Model):
    name = CharField()
    birthday= DateTimeField()
    is_relative= BooleanField()

    class Meta:
        database=db

class Pet(Model):
    owner=ForeignKeyField(Person,related_name="pets")
    name=CharField()
    animal_type=CharField()

    class Meta:
      database=db

def create_and_connect():
    db.connect()
    db.create_tables([Person],safe=True)
    #db.create_tables([Pet],safe=True)

def create_family_members():
    uncle_tommy= Person("Tommy",birthday= date(1995,12,26),is_relative=True)
    uncle_tommy.save()
    grandma_ana=Person.create(name="Ana",birthday=date(1935,6,22),is_relative=False)
create_and_connect()
create_family_members()   
print(*database)



C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/Usuario/Desktop/Udemy-Python/Peewee/persons.py
Traceback (most recent call last):

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2981, in execute_sql

    cursor.execute(sql, params or ())

sqlite3.IntegrityError: NOT NULL constraint failed: person.name



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

  File "C:/Users/Usuario/Desktop/Udemy-Python/Peewee/persons.py", line 39, in <module>

    create_family_members()   

  File "C:/Users/Usuario/Desktop/Udemy-Python/Peewee/persons.py", line 36, in create_family_members

    uncle_tommy.save()

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 6311, in save

    pk = self.insert(**field_dict).execute()

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 1796, in inner

    return method(self, database, *args, **kwargs)

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 1867, in execute

    return self._execute(database)

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2599, in _execute

    return super(Insert, self)._execute(database)

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2336, in _execute

    cursor = database.execute(self)

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2994, in execute

    return self.execute_sql(sql, params, commit=commit)

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2988, in execute_sql

    self.commit()

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2759, in __exit__

    reraise(new_type, new_type(*exc_args), traceback)

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 183, in reraise

    raise value.with_traceback(tb)

  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 2981, in execute_sql

    cursor.execute(sql, params or ())

peewee.IntegrityError: NOT NULL constraint failed: person.name


Process finished with exit code 1
Responder
#2
Hola. Lo que te está indicando el error es que en algún lado estás creando una fila en la tabla "person" sin indicar el campo "name". Puede que sea en esta línea:

  1. uncle_tommy= Person("Tommy",birthday= date(1995,12,26),is_relative=True)


Debería ser:

  1. uncle_tommy= Person(name="Tommy", birthday= date(1995,12,26),is_relative=True)


Saludos
Responder


Salto de foro:


Usuarios navegando en este tema: 1 invitado(s)