peewee.IntegrityError:NOT NULL约束失败

时间:2015-04-30 21:36:38

标签: python flask peewee

我正在尝试创建一个关系数据库,但是当调用构造函数时,我得到以下错误peewee.IntegrityError: NOT NULL constraint failed language.content_id。我尝试了许多不同的方法来完成这项工作,但在调用models.Language.create()时出现问题。插入表中的其他调用工作正常。任何想法如何解决这个问题?请帮忙。

这是我的代码:

models.py

class User(UserMixin, Model):
    username = CharField(unique=True)
    email = CharField(unique=True)
    password = CharField(max_length=100)
    joined_at = DateTimeField(default=datetime.datetime.now)
    is_admin = BooleanField(default=False)

    class Meta:
        database = DATABASE
        order_by = ('-joined_at',)

    def get_post(self):
        return Post.select().where(Post.user == self)


    @classmethod
    def create_user(cls, username, email, password, admin=False):
        try:
            cls.create(
                username=username,
                email=email,
                password=generate_password_hash(password),
                is_admin=admin)
        except IntegrityError:
            raise ValueError("User already exists")


class Post(Model):
    content = TextField()

    class Meta:
        database = DATABASE

class Category(Model):
    category = TextField()

    class Meta:
        database = DATABASE

class Language(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    content = ForeignKeyField(rel_model=Post, related_name='contents')
    category = ForeignKeyField(rel_model=Category, related_name='topic')
    user = ForeignKeyField(
        rel_model=User,
        related_name='units'
    )
    language = TextField()

    class Meta:
        database = DATABASE

app.py

@app.route('/new_post', methods=('GET', 'POST'))
@login_required
def post():
    form = forms.PostForm()
    if form.validate_on_submit():
        models.Post.create(content=form.content.data)
        models.Category.create(category=form.choice.data.strip())
        models.Language.create(user=g.user._get_current_object(),
                               language=form.language.data.strip())
        flash("Message posted! Thanks!", "success")
        return redirect(url_for('index'))
    return render_template('post.html', form=form)

0 个答案:

没有答案