Python sqlalchemy:没有主键和重复值的表?
我正在使用第三方使用 sqlalchemy 制作的现有数据库。但是,我遇到了麻烦,因为表没有主键,更糟糕的是,它们每行都有重复的元素,所以我无法选择现有列作为主键。这些表有两列:两列都有非唯一值。
我当前的代码是(MirnaTable
是我的映射类,基本上只是一个骨架,没有其他东西)
connection = create_engine("sqlite:///targets.sqlite")
metadata = MetaData(bind=connection)
db_table = Table("miranda", metadata,
Column("id", Integer, primary_key=True),
autoload=True)
mapper(MirnaTable, db_table)
Session = sessionmaker(connection)
session = Session()
然后我尝试例如发出
all_records = session.query(MirnaTable).all()
并且我得到
sqlalchemy.exc.OperationalError: (OperationalError) no such column: miranda.id
u'SELECT miranda.gene_id AS miranda_gene_id, miranda."mature_miRNA" AS
"miranda_mature_miRNA", miranda.id AS miranda_id \nFROM miranda' ()
所以当然找不到 id 列。关于我做错了什么有什么想法吗?提前致谢。
编辑:根据要求,这里是表中的一个示例(直接从 sqlite 检索):
gene mature_miRNA
---- -------------
80205 hsa-miR-200c
80205 hsa-miR-200c
9693 hsa-miR-200c
9693 hsa-miR-200c
9881 hsa-miR-200c
9710 hsa-miR-200c
9750 hsa-miR-200c
I'm using an existing database made by a third party with sqlalchemy. However, I'm having trouble as the tables do not have primar keys, and what's worse, they have duplicate elements for each row, so I can't pick an existing column as primary key. The tables have two columns: both have non-unique values.
I tried to monkey-patch the table as per http://www.blog.pythonlibrary.org/2010/09/10/sqlalchemy-connecting-to-pre-existing-databases/ but apparently this does not work (see below)
My current code is (MirnaTable
is my mapped class, basically just a skeleton with nothing else)
connection = create_engine("sqlite:///targets.sqlite")
metadata = MetaData(bind=connection)
db_table = Table("miranda", metadata,
Column("id", Integer, primary_key=True),
autoload=True)
mapper(MirnaTable, db_table)
Session = sessionmaker(connection)
session = Session()
Then I try for example issuing
all_records = session.query(MirnaTable).all()
And I get
sqlalchemy.exc.OperationalError: (OperationalError) no such column: miranda.id
u'SELECT miranda.gene_id AS miranda_gene_id, miranda."mature_miRNA" AS
"miranda_mature_miRNA", miranda.id AS miranda_id \nFROM miranda' ()
So of course the id column isn't found. Any ideas on what I'm doing wrong? Thanks in advance.
EDIT: As requested, here is an example from the table (retrieved directly from sqlite):
gene mature_miRNA
---- -------------
80205 hsa-miR-200c
80205 hsa-miR-200c
9693 hsa-miR-200c
9693 hsa-miR-200c
9881 hsa-miR-200c
9710 hsa-miR-200c
9750 hsa-miR-200c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您误解了您引用的帖子。您必须选择一个现有列并将其定义为主列。还可以通过将它们全部放入定义中来设置复合主键。在你的例子中,我认为一个基因有几个成熟的 microRNA,所以主键可能应该由
(gene_id,mature_miRNA)
对组成。由于表中没有更多字段,因此不需要autoload=True
标志。我不知道你表中字段的类型,所以如果它们不是整数,请适当更改它们。
You've misinterpreted the post you refer to. You have to choose an existing column and define it as primary. It's also possible to setup composite primary key by putting them all in definition. In your case I think a gene has several mature microRNA, so the primary key should probably consist of
(gene_id, mature_miRNA)
pair. Since there is no more fields in the table, there is no need inautoload=True
flag.I don't know the types of fields in your table, so change them appropriately if they are not integer.
将 sqlite 数据库的列 id 更改为 rowid
原始:
修改:
Change Column id to rowid for sqlite database
original:
modified:
首先确保 id 列存在,然后尝试大小写。
还可以为表设置两个主键:
但是,如果任何其他行与该行完全匹配,则可能会导致更新异常。您可能最好重新创建表并插入您自己的 PK 列
Make sure that the id column exists first off and then that you have tried both upper and lower case.
It is also possible to set two primary keys for the table:
However this may cause update anomalies if any other row matches this row exactly. You might be best recreating the table and inserting your own PK column