我正在使用这里描述的MySQLicious类型模式来建立一个简单的标记系统.我已经在4个不同的SO线程中阅读了一些标记模式的替代实现,这最适合我的需求.
条目集合有标签"apple banana orange"和"strawberry banana lemon",我正在尝试找到Elixir/SQLAlchemy等效语句
SELECT * FROM table WHERE tags LIKE "%banana%";
Run Code Online (Sandbox Code Playgroud)
我无法找到任何这样的方法来构造Class.query.filter/filter_by()命令,并且无法在文档中看到任何模块的类似方法.有一个简单的方法吗?或者我应该只使用原始SQL.
额外问题:MySQLicious架构的一个缺点是我可能希望搜索"%apple%"但返回"菠萝".是否有高级方法来处理此测试用例?或者我应该在每个查询中包含一个前导空格?
nB:对于那些关心的人来说,这是我对数据库的第一次体验,所以我可能会忽略其他线程中提到的模式的核心优势.我的应用程序用于记录一个或两个关于已完成任务的句子,列[TaskID,Tags,Notes,StartTime,StopTime,TimeTaken],有点像简单的日记.主要用于教程目的.我希望能够通过单个标签进行搜索,以便大致了解我在给定任务上花费的时间.
我有一个使用SA的association_proxy建模的多对多(开发人员和项目)关系.集合(每个项目的开发人员和每个开发人员的项目)工作正常,但我需要过滤关联本身的属性(状态).像这样的东西(不起作用):
activeDevelopers = s.query(Developer).filter_by(Developer.developerProjects.status == 'active').all()
Run Code Online (Sandbox Code Playgroud)
我错过了什么?这是完整的测试代码:
import logging
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import relation, mapper, sessionmaker
from sqlalchemy.sql import *
from sqlalchemy.ext.associationproxy import association_proxy
log = logging.getLogger('nm_test')
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s,%(msecs)03d %(levelname)s [%(filename)s.%(funcName)s @ %(lineno)d.%(thread)d] %(message)s')
engine = create_engine('sqlite:///:memory:', echo = False, echo_pool = False)
meta = MetaData()
meta.bind = engine
developer_table = Table('developer', meta,
Column('id', Integer, primary_key=True, autoincrement = False),
Column('name', String),
)
project_table = Table('project', meta,
Column('id', …Run Code Online (Sandbox Code Playgroud) 我有下一个查询.
item = [item.export_simple()
for item in session.query(Item)
.filter(and_(
Item.companyId == company_id,
or_(
True if search == "" else None,
or_(*[Item.name.like('%{0}%'.format(s)) for s in words]),
or_(*[Item.code.like('%{0}%'.format(s)) for s in words])
))).order_by(Item.name)]
Run Code Online (Sandbox Code Playgroud)
还有这个.
if type == "code":
src = [Item.code.like('%{0}%'.format(s)) for s in words]
elif type == "name":
src = [Item.name.like('%{0}%'.format(s)) for s in words]
session.query(Item)
.filter(and_(
Item.companyId == company_id,
Item.typeItem == item_type,
or_(
True if search == "" else None,
or_(*src)
)))
Run Code Online (Sandbox Code Playgroud)