My code errors out with the following code:
from .db import db, environment, SCHEMA, add_prefix_for_prod
from sqlalchemy import Index
class Capstone(db.Model):
__tablename__ = "capstones"
if environment == "production":
__table_args__ = (
{'schema': SCHEMA},
Index('capstone_user_id_idx', 'user_id')
)
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), nullable=False)
url = db.Column(db.String(150))
description = db.Column(db.String(1000), nullable=False)
cloned_from = db.Column(db.String(75), nullable=False)
user_id = db.Column(db.String(75), nullable=False)
created_at = db.Column(db.TIMESTAMP)
# relations
capstone_images = db.relationship("CapstoneImage", back_populates="capstone")
reviews = db.relationship("Review", back_populates="capstone")
def to_dict(self):
return {
...
}
i get the error: sqlalchemy.exc.ArgumentError: 'SchemaItem' object, such as a 'Column' or a 'Constraint' expected, got {'schema': 'captracker'}
Because I only need indexing for user_id, I can add index=True
to the user_id column and remove the Index() from the tuple __table_args__ = {'schema': SCHEMA}
, this fixes the error. Why does this work and not the previous setup?
Martynodlrr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.