Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask站点与SQlite一起工作,但在连接到Postgres时会中断_Python_Postgresql_Sqlalchemy - Fatal编程技术网

Python Flask站点与SQlite一起工作,但在连接到Postgres时会中断

Python Flask站点与SQlite一起工作,但在连接到Postgres时会中断,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我的网站本质上是一个博客网站——用户上传一篇文章,每篇文章都有分类标签。我使用SQlite db构建站点,当我切换到Postgres时,我在上传新帖子时开始出现以下错误: sqlalchemy.exc.DataError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (psyco

我的网站本质上是一个博客网站——用户上传一篇文章,每篇文章都有分类标签。我使用SQlite db构建站点,当我切换到Postgres时,我在上传新帖子时开始出现以下错误:

sqlalchemy.exc.DataError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
(psycopg2.errors.StringDataRightTruncation) value too long for type character varying(20)
我怀疑问题可能出在
add_tags()
db.session.commit()

以下是Post&Tags模型供参考:

class Post(db.Model): #one-to-many relationship because 1 user can have multiple posts, but post can have 1 author
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) #pass in function as argument (utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    notebook_file = db.Column(db.String(20), nullable=False, default='default.ipynb')  # hash unique image files each 20 chars long
    notebook_type = db.Column(db.String(20), nullable=False, default='Jupyter Notebook')
    notebook_html = db.Column(db.Text, nullable=False, default='No Notebook File')
    tags = db.relationship('Tags', secondary=relationship_table, backref=db.backref('posts', lazy='dynamic'))

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}')"


class Tags(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True, nullable=False)
    description = db.Column(db.Text)

我不明白为什么我的代码会给我这个
DataError
。任何指点都将不胜感激

你可能需要更新你的
notebook\u文件
notebook\u类型
db.Text
,除非你真的需要约束(在这种情况下,你可以将
检查
约束添加到你的数据库中。另外,
varchar(N)
是(还有许多其他类似的博客文章)。此外,在SQLite中,
varchar(N)
不是,这可能解释了为什么您以前能够不出错

否则,请更新您的原始帖子,证明您在未尝试输入大于20个字符的
笔记本文件
笔记本类型
时收到了错误消息


披露:我为

工作就是这样。从字符串改为文本,效果很好。非常感谢。
class Post(db.Model): #one-to-many relationship because 1 user can have multiple posts, but post can have 1 author
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) #pass in function as argument (utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    notebook_file = db.Column(db.String(20), nullable=False, default='default.ipynb')  # hash unique image files each 20 chars long
    notebook_type = db.Column(db.String(20), nullable=False, default='Jupyter Notebook')
    notebook_html = db.Column(db.Text, nullable=False, default='No Notebook File')
    tags = db.relationship('Tags', secondary=relationship_table, backref=db.backref('posts', lazy='dynamic'))

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}')"


class Tags(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, unique=True, nullable=False)
    description = db.Column(db.Text)