- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个应用程序来替换浏览器中的当前选项卡管理器。我在组 - 表主题和选项卡 - 表选项卡之间创建了一个简单的一对多关系。如果我删除它,我希望能够自动删除主题的 child 。这是我目前拥有的:
from flask import request, redirect, url_for, render_template, Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import exc
from flask_whooshee import Whooshee
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
whooshee = Whooshee(app)
@whooshee.register_model('topic_name')
class Topic(db.Model):
__tablename__ = 'topic'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
topic_name = db.Column(db.String, unique=True, nullable=False)
topic_created = db.Column(db.DateTime, default=datetime.utcnow)
topic_changed = db.Column(db.DateTime, default=datetime.utcnow)
topic_tabs = db.relationship('Tab', backref='topic', cascade='all, delete-orphan', lazy='dynamic')
@whooshee.register_model('tab_name', 'tab_link', 'tab_description')
class Tab(db.Model):
__tablename__ = 'tab'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
tab_name = db.Column(db.String, nullable=False)
tab_link = db.Column(db.String, unique=True, nullable=False)
tab_favicon = db.Column(db.String)
tab_description = db.Column(db.String)
tab_created = db.Column(db.DateTime, default=datetime.utcnow)
tab_changed = db.Column(db.DateTime, default=datetime.utcnow)
topic_id = db.Column(db.Integer, db.ForeignKey('topic.id'))
@app.route('/', methods=['GET'])
def index():
return "Test"
if __name__ == '__main__':
db.create_all()
try:
db.session.commit()
except exc.IntegrityError as e:
print(str(e))
db.session.rollback()
top = Topic(topic_name='Test')
db.session.add(top)
try:
db.session.commit()
except exc.IntegrityError as e:
print(str(e))
db.session.rollback()
top2 = Topic(topic_name='Test2')
db.session.add(top2)
try:
db.session.commit()
except exc.IntegrityError as e:
print(str(e))
db.session.rollback()
t1 = Tab(tab_name='t1', tab_link='t1l', tab_favicon='t1f', tab_description='t1d', topic_id=top.id)
t2 = Tab(tab_name='t2', tab_link='t2l', tab_favicon='t2f', tab_description='t2d', topic_id=top.id)
t3 = Tab(tab_name='t3', tab_link='t3l', tab_favicon='t3f', tab_description='t3d', topic_id=top.id)
db.session.add(t1)
db.session.add(t2)
db.session.add(t3)
try:
db.session.commit()
except exc.IntegrityError as e:
print(str(e))
db.session.rollback()
import uuid
app.secret_key = str(uuid.uuid4())
app.run(debug=True)
问题是当我尝试在数据库中添加某些内容时出现完整性错误,但我不确定会出现什么问题。如果我停止服务器,我还想将以前的记录保存在数据库中,我该如何实现?
python script_name.py
在一个空文件夹中。
(sqlite3.IntegrityError) UNIQUE constraint failed: topic.topic_name [SQL: 'INSERT INTO topic (topic_name, topic_created, topic_changed) VALUES (?, ?, ?)'] [parameters: ('Test', '2018-09-03 05:21:08.728564', '2018-09-03 05:21:08.728564')] (Background on this error at: http://sqlalche.me/e/gkpj)
(sqlite3.IntegrityError) UNIQUE constraint failed: topic.topic_name [SQL: 'INSERT INTO topic (topic_name, topic_created, topic_changed) VALUES (?, ?, ?)'] [parameters: ('Test2', '2018-09-03 05:21:08.730562', '2018-09-03 05:21:08.730562')] (Background on this error at: http://sqlalche.me/e/gkpj)
(sqlite3.IntegrityError) UNIQUE constraint failed: tab.tab_link [SQL: 'INSERT INTO tab (tab_name, tab_link, tab_favicon, tab_description, tab_created, tab_changed, topic_id) VALUES (?, ?, ?, ?, ?, ?, ?)'] [parameters: ('t1', 't1l', 't1f', 't1d', '2018-09-03 05:21:08.733561', '2018-09-03 05:21:08.733561', None)] (Background on this error at: http://sqlalche.me/e/gkpj)
最佳答案
您的脚本是 WSGI 应用程序,因此运行为 __main__
.如果您使用 debug=True
作为 app.run()
的参数服务器将启动,设置 Debug模式:打开并重新启动 with stat
,执行 __name__ == '__main__'
再次阻止(每次更改脚本并在服务器运行时保存它时再次阻止)。
当您在一个空的/已删除的数据库上启动您的应用程序时,第一次 __name__ == '__main__'
块被执行,它创建数据库并插入两个 Topic
和三个 Tab
您在该块中创建的对象。然后它设置 Debug模式并重新启动,再次执行该块,尝试第二次插入这五个对象。
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello, World!"
if __name__ == '__main__':
print(__name__)
app.run(debug=True)
__main__ # <-- fist time around it creates DB and objects
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat # <-- re-executing your script
__main__ # <-- trying to create the same objects again, violating the UNIQUE constraint
* Debugger is active!
* Debugger PIN: 302-544-855
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
关于python - Flask-SQLAlchemy:sqlite3 IntegrityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55782001/
我在 app_name 文件夹的根目录下的 django 应用程序中运行脚本;就像在 app_name>app_name 中一样。 在脚本的顶部,我有这个: import os import sys
我正在使用 sqlite 数据库,我将模型声明为: class User(db.Model): __tablename__ = 'users' id = db.Column(db.In
我正在使用 sqlite 数据库并且我在这个要点中声明了模型 https://gist.github.com/mmahesh/7245561 我添加了一个带有事务管理器的模型实例作为 with tra
我的网页的注册 View (如下)通过 Django 的身份验证模型 User 创建了一个新用户,然后使用该新创建的用户 ID 通过我自己的模型 account.Users 添加更多信息。我的问题是,
我正在尝试使用复合主键创建一个表。它有一个整数和两个字符串字段作为键。当我提交 session 时,它会引发 IntegrityError .我在做什么有什么问题? class Targets(db.
当我尝试将新条目保存到名为“config”的表中时遇到了这个问题, class Config(models.Model): ident = models.CharField(max_lengt
找不到解决方案。我现在传递一个元组列表,其中包含要插入的信息而不是从其他表中提取的信息,问题就消失了。 我目前正在编写将要编辑表格的代码。我有几行信息要插入,但无论顺序如何,我总是在第四个元素上收到
我有 python 线程应用程序 + Postgres。我正在使用 Django 的 ORM保存到 Postgres.. 我有并发保存调用。偶尔有 2 个线程保存导致问题的相同主键。 Postgres
有人可以向我解释如何正确测试 Postgres 数据库错误,尤其是 IntegrityError。例如我有下一个测试: class TestSlugs(TestCase): # This sl
我在尝试删除 User 时收到 IntegrityError: IntegrityError: (1451, 'Cannot delete or update a parent row: a fo
在 python 中,我正在使用 importmany 填充 SQLITE 数据库,因此我可以一次导入数万行数据。我的数据包含在元组列表中。我在数据库中设置了我想要的主键。 我遇到的问题是主键错误会引
每当尝试附加文件时,我的应用程序(本地)都会引发ActiveStorage::IntegrityError错误。我如何摆脱这个错误? 我只有一个has_one_attached,但我不知道该错误是如何
我想插入几个User数据库中的行。我真的不在乎插入是否成功,只要我得到通知,在这两种情况下我都能做到,那么哪一个在性能(主要是速度)方面更好? 始终插入行(通过调用模型的 save 方法)并捕获潜在的
我开始使用 factory_boy 包,所以我设置了一些工厂并想测试创建的对象不会引发任何验证错误。 这是我正在使用的 mixin,它基本上从模块中获取每个工厂,创建一个实例,然后测试 .full_c
我有一个模型 LucyGuide,它通过 OneToOneField 扩展了 Django 的 User 模型: class LucyGuide(models.Model): user = m
我有一个模型 LucyGuide,它通过 OneToOneField 扩展了 Django 的 User 模型: class LucyGuide(models.Model): user = m
我正在尝试在 Django 中创建配方/成分模型 在我的 models.py 中我得到了 class Ingredient(models.Model): name = models.CharF
我正在使用 Django 构建一个简单的博客应用程序。我想实现使用表单添加新博客的功能。出现一些问题。 这是我的 models.py from django.db import models from
我知道还有许多其他问题与完全相同的问题有关,但我已经尝试过他们的答案,但到目前为止没有一个有效。 我正在尝试从与其他表有关系的表中删除记录。这些表中的外键是 nullable=false ,因此尝试删
我正在与普通/ guest 用户一起处理我的结帐 View ,但很难解决完整性错误。想法是让访客用户仅使用电子邮件注册才能结帐,我需要将用户电子邮件设置为唯一。 模型.py from django.c
我是一名优秀的程序员,十分优秀!