![](/img/trans.png)
[英]SQLAlchemy gives error Exception: No searchable columns defined for model Post
[英]searchable columns with sqlalchemy and postgres
我是数据库的初学者,我不确定是否正确理解它们。
据我所知,给定一个包含多列的表,我可以进行如下查询: SELECT * FROM table WHERE col1>3
。
该查询的复杂度为N
为了使搜索更加有效,我可以使用col1
作为索引。 在这种情况下,同一查询应具有复杂度log(N)
。
现在,据我了解,可以在sqlalchemy中将一列设置为主键来进行搜索。
如果这是正确的,我不明白为什么我不能将具有重复项的列设置为主键。
例如:
import sqlite3
from sqlalchemy import *
metadata = MetaData()
table = Table('example', metadata,
Column('col1', Integer, primary_key=True),
Column('col2', Integer))
engine = create_engine('sqlite:///:memory:', echo=True)
con = engine.connect()
table.create(engine, checkfirst=True)
data = [{'col1':1, 'col2':2}, {'col1':3, 'col2':4}, {'col1':3, 'col2':4}]
ins = table.insert().values(data)
con.execute(ins)
print list(con.execute("SELECT * FROM example"))
returns
IntegrityError: (sqlite3.IntegrityError) PRIMARY KEY must be unique [SQL: u'INSERT INTO example (col1, col2) VALUES (?, ?), (?, ?), (?, ?)'] [parameters: (1, 2, 3, 4, 3, 4)]
如何使非唯一列在log(N)中可搜索?
编辑:该示例是使用sqlite编写的,但实际上我正在使用postgres。
现在,据我了解,可以在sqlalchemy中将一列设置为主键来进行搜索。
主键列会自动建立索引,但是您经常需要索引非唯一列。 您可以使用index
关键字参数来做到这一点:
table = Table('example', metadata,
Column('col1', Integer, index=True),
Column('col2', Integer)
)
您可以在日志文件中看到相应的SQL:
INFO sqlalchemy.engine.base.Engine CREATE INDEX ix_example_col1 ON example (col1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.