简体   繁体   English

是否可以使用 SQLAlchemy 在数据库中创建列表或数组?

[英]Is it possible to have a list or array in a database using SQLAlchemy?

I'm trying to build a system that allows users to make 'projects'.我正在尝试构建一个允许用户制作“项目”的系统。 These projects have a fairly simple syntax, they just need an ID, a name, optionally a description and the participants.这些项目的语法相当简单,它们只需要一个 ID、一个名称、可选的描述和参与者。

Because I want users to be able to add or remove other users from the project without having to input the entire list of users again, I want to make use of a string or array or some such method instead of a string.因为我希望用户能够在项目中添加或删除其他用户而不必再次输入整个用户列表,所以我想使用字符串或数组或类似的方法而不是字符串。

However, I'm stuck with trying to input it.但是,我坚持尝试输入它。 I initially tried a regular list, but SQLalchemy didn't accept that.我最初尝试了一个常规列表,但 SQLalchemy 不接受。 After a google search, it appears that's not possible?谷歌搜索后,似乎这是不可能的? Unless I simply haven't come upon it.除非我根本没有想到它。

I am now trying to use an Array instead.我现在正在尝试改用数组。 My current code looks like this:我当前的代码如下所示:

class DBProject(db.Model):
__tablename__ = 'project'
    project_id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(128))
    project_description = db.Column(db.String(255), nullable=True)
    project_participants = db.Column(db.ARRAY(db.Integer))

But this gives the error: in _compiler_dispatch raise exc.UnsupportedCompilationError(visitor, cls) sqlalchemy.exc.CompileError: (in table 'project', column 'project_participants'): Compiler can't render element of type但这给出了错误:在 _compiler_dispatch raise exc.UnsupportedCompilationError(visitor, cls) sqlalchemy.exc.CompileError: (in table 'project', column 'project_participants'): 编译器无法呈现类型的元素

Before, I tried leaving (db.Integer) out or replacing it with just Integer (because I had seen this with other people with similar problems) like this:之前,我尝试将 (db.Integer) 排除在外或仅用 Integer 替换它(因为我在其他有类似问题的人身上看到过这个),如下所示:

project_participants = db.Column(db.ARRAY(Integer))

Which gives the error that 'Integer' is not defined or, in case of leaving it out altogether, this error:这给出了 'Integer' 未定义的错误,或者如果完全忽略它,则会出现此错误:

TypeError: init () missing 1 required positional argument: 'item_type' TypeError: init () missing 1 required positional argument: 'item_type'

I'm hoping to add the array to the database so that I can use it to append new users or delete users without having to make the system's user input all allowed users all over again when he just wants to add or delete one.我希望将数组添加到数据库中,这样我就可以使用它来追加新用户或删除用户,而不必在他只想添加或删除一个用户时再次让系统的用户输入所有允许的用户。

First i recommend you strongly to save your participants data in an additional table.首先,我强烈建议您将参与者数据保存在一个附加表中。 You can add am:n relation between your DBProject-Table and your Participants-Table.您可以在 DBProject-Table 和 Participants-Table 之间添加 am:n 关系。 Anything else would be against any best practice use of databases.任何其他事情都会违反数据库的任何最佳实践使用。 Saving your participants as an Array in your table makes it impossible or at least very uncomfortable to filter by participants in a SQL-query.将参与者保存为表中的数组使得在 SQL 查询中按参与者进行过滤变得不可能或至少非常不舒服。

But if you have a good reason to ignore that recommendation you can use pickle to make SQLAlchemy transform your array into a string while writing into your database.但是,如果您有充分的理由忽略该建议,则可以使用 pickle 使 SQLAlchemy 在写入数据库时将数组转换为字符串。

class DBProject(db.Model):
    __tablename__ = 'project'
    project_id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(128))
    project_description = db.Column(db.String(255), nullable=True)
    project_participants = db.Column(db.PickleType, nullable=True)

Using that construct you can basicalliy put any object (if not exceeding a database specific maximum size) into a database field.使用该结构,您基本上可以将任何对象(如果不超过数据库特定的最大大小)放入数据库字段。

Save data:保存数据:

dbproject_object = DBProject()
dbproject_object.name = "a_name"
dbproject_object.participants = ["you","him","another one"]
session.add(dbproject_object)
session.commit()

Read Data:读取数据:

participants_array = db.session.query(DBProject).filter(name == "a_name").one().participants 

Result:
participants_array : ["you","him","another one"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM