from peewee import *

db = MySQLDatabase('my_database', user='xxx', password='P@x',
                   host='xxxxxx', port=3306)


class User(Model):
    name = CharField()
    email = CharField()

    class Meta:
        database = db


class Score(Model):
    num = IntegerField()
    owner = ForeignKeyField(User, backref='score')

    class Meta:
        database = db


db.create_tables([User])
db.create_tables([Score])

######################插入数据######################
# 方式一
user1 = User.create(name="吴森", email="wusen@163.com")
# 方式二
user2 = User(name="萧炎", email="xiaoyan@163.com")
user2.save()
# 方式三 批量创建
users = [
    User(name="批量1", email="piliang1@163.com"),
    User(name="批量2", email="piliang2@163.com"),
]
User.bulk_create(users)