[英]How to define a column as an Array of any type in POSTGRESQL FlaskSQLAlchemy?
I am using flask_sqlalchemy
and I want to define column prompts to be an array which accepts values of any data type, particularly string or json .我正在使用
flask_sqlalchemy
并且我想将列提示定义为一个数组,它接受任何数据类型的值,特别是string或json 。
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
class PreannotationPrompts(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique = True)
type = db.Column(db.String(100))
'''
Example of values prompts should hold
[
{"key": "value", "key2": []},
'Hello',
{"key": "value", "key2": []},
'Hi'
]
'''
prompts = db.Column(db.String(1000)) # How to define ?
How can I define prompts column appropriately in this case?在这种情况下如何适当地定义提示列?
SQL is a typed language, you you cannot have a column that accepts "any type". SQL 是一种类型化语言,您不能拥有接受“任何类型”的列。 You should rethink your data model.
您应该重新考虑您的数据模型。 Avoid arrays as column data types.
避免将数组作为列数据类型。
If you anticipate fluid model within a column you probably should use JSONB type.如果您预计列中有流体模型,您可能应该使用 JSONB 类型。 You can store a dict or an array without a priori declared element's type.
您可以在没有先验声明元素类型的情况下存储字典或数组。
from sqlalchemy.dialects.postgresql import JSONB
prompts = db.Column(JSONB, default=list)
However, using a schemaless column (like JSONB) can indicate that you have a problem with properly designed data model.但是,使用无模式列(如 JSONB)可能表明您在正确设计的数据模型方面存在问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.