如何在 SQLAlchemy 模型中创建数组或列表列?
How to create an array or list column in SQLAlchemy model?
我知道可以在 postgres 中创建字符串数组,但我想在 sqlalchemy 中创建一个包含列表或数组列的模型,但我不知道如何
请查看下方代码
class Question(Base):
__tablename__= 'questions'
id = Column(Integer, nullable=False, primary_key=True)
question = Column(String,nullable=False)
options = Column([String],nullable=False)
answer = Column(String,nullable=False)
created_at = Column(TIMESTAMP(timezone=true),server_default=text('now()')
您需要使用:
from sqlalchemy.dialects.postgresql import ARRAY
这里:
from datetime import datetime
from sqlalchemy import *
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Question1(Base):
__tablename__= 'questions'
id = Column(Integer, nullable=False, primary_key=True)
question = Column(String,nullable=False)
options = Column(ARRAY(String),nullable=False)
answer = Column(String,nullable=False)
示例:
Python 3.8.2 (default, Dec 21 2020, 15:06:04)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> from sqlalchemy import *
>>> from sqlalchemy.dialects.postgresql import ARRAY
>>> from sqlalchemy.ext.declarative import declarative_base
>>> Base = declarative_base()
>>> class Question1(Base):
... __tablename__= 'questions'
... id = Column(Integer, nullable=False, primary_key=True)
... question = Column(String,nullable=False)
... options = Column(ARRAY(String),nullable=False)
... answer = Column(String,nullable=False)
...
>>>
我知道可以在 postgres 中创建字符串数组,但我想在 sqlalchemy 中创建一个包含列表或数组列的模型,但我不知道如何
请查看下方代码
class Question(Base):
__tablename__= 'questions'
id = Column(Integer, nullable=False, primary_key=True)
question = Column(String,nullable=False)
options = Column([String],nullable=False)
answer = Column(String,nullable=False)
created_at = Column(TIMESTAMP(timezone=true),server_default=text('now()')
您需要使用:
from sqlalchemy.dialects.postgresql import ARRAY
这里:
from datetime import datetime
from sqlalchemy import *
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Question1(Base):
__tablename__= 'questions'
id = Column(Integer, nullable=False, primary_key=True)
question = Column(String,nullable=False)
options = Column(ARRAY(String),nullable=False)
answer = Column(String,nullable=False)
示例:
Python 3.8.2 (default, Dec 21 2020, 15:06:04)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> from sqlalchemy import *
>>> from sqlalchemy.dialects.postgresql import ARRAY
>>> from sqlalchemy.ext.declarative import declarative_base
>>> Base = declarative_base()
>>> class Question1(Base):
... __tablename__= 'questions'
... id = Column(Integer, nullable=False, primary_key=True)
... question = Column(String,nullable=False)
... options = Column(ARRAY(String),nullable=False)
... answer = Column(String,nullable=False)
...
>>>