Column marking in sqlalchemy tables

I would like to add a custom search attribute to the columns in the sqlalchemy model. The goal is to retrieve data only for these columns (using ModelFoo .__ table __. Columns) and put the data in solr. Is there a way to mark some columns using a custom attribute?

class ModelFoo(AppBase):
    __tablename__ = 'foo'
    id = Column("id", Integer, primary_key=True, autoincrement=True)
    os = Column(String, nullable=False, searchable=True)
    platform = Column(String, searchable=True)

By default, I get the following error when I try to do the following:

sqlalchemy.exc.ArgumentError: Unknown arguments passed to Column: ['searchable']

I am looking for a general way to add only search columns in solr, something like these lines:

for table in Base.metadata.tables.values():
    keys = [str(key) for key in table.columns if key.searchable] 
    solr.add(session.query(*keys).all())

in the code above, I am looking for a short solution or an alternative for working with "key.searchable". Hope this clarifies the issue.

+3
source share
2 answers

, :

class ModelFoo(Base):
    __tablename__ = 'foo'
    id = Column("id", Integer, primary_key=True, autoincrement=True)
    os = Column(String, nullable=False)
    platform = Column(String)
    search_cols = ["os", "value"]

for k, v in list(Base._decl_class_registry.items()):
    if (hasattr(v, "search_cols")):
        cols = [getattr(v, val) for val in v.search_cols]
        query = sess.query(*cols)
        solr.add(query.all())
+2

, , - :

si.add(Book.objects.all())

Book.objects.all() - ORM. , , . , - SQLAlchemy , . :

si.add(session.query(ModelFoo.os, ModelFoo.platform).all())
0

All Articles