Вопрос

I'd like to add a custom attribute "searchable" to the columns in an sqlalchemy model. The purpose is to extract data for just these columns (using ModelFoo.__ table__.columns) and put the data into solr. Is there a way I can mark certain 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 the above:

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

I am looking for a generic way to add only "searchable" columns to solr, something along 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 above code, I am looking for some short solution or alternative to get "key.searchable" to work. Hope this clarifies the question.

Это было полезно?

Решение

I solved this using a separate attribute in model:

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())

Другие советы

I'm a little unclear on your question but it looks like your are trying to do something like this from the link you provided:

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

Where Book.objects.all() is a list of records from an ORM mapped table. Note that the docs say a list of objects is also acceptable. I think to solution here is to use the SQLAlchemy query method to build records with only the fields you want. Using your example with would look like this:

si.add(session.query(ModelFoo.os, ModelFoo.platform).all())
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top