简体   繁体   中英

Enforce Not Null field in MariaDB using Peewee

Well, I'm trying to use Peewee for a small project. I need fields text and url (both longtext in MariaDB) to be not null and for not null I understand not to allow a null value to be stored in the field , so I did this:

database = MySQLDatabase("mydatabase", user="", password="123justkidding", host="localhost")

class MyTable(Model):
    text = TextField(null = False)
    url = TextField(null = False, max_length = 100)

    class Meta:
        database = database

and then, of course, I created the table and I started adding values, lots of values. The thing is that there are some empty values on text and url fields which I don't want them to be. I ran this query:

SELECT `table`.`text`, `table`.`url` FROM `table` WHERE `table`.`text`="" AND `table`.`url`=""

and gave me four rows. So, What am I missing guys?

Also, I notice that MariaDB has Text , Medium Text , LongText as data-types and it appears to me that TextField in Peewee is default for LongText in MariaDB . Any way I can change that?

Thanks in advance.

"" is empty string. NULL is something different entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM