Python sqlalchemy:没有主键和重复值的表?

发布于 2025-01-05 11:31:03 字数 1471 浏览 0 评论 0原文

我正在使用第三方使用 sqlalchemy 制作的现有数据库。但是,我遇到了麻烦,因为表没有主键,更糟糕的是,它们每行都有重复的元素,所以我无法选择现有列作为主键。这些表有两列:两列都有非唯一值。

我尝试按照 http://www.blog.pythonlibrary.org/2010/09/10/sqlalchemy-connecting-to-pre-existing-databases/">http:// /www.blog.pythonlibrary.org/2010/09/10/sqlalchemy-connecting-to-pre-existing-databases/ 但显然这不起作用(见下文)

我当前的代码是(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难愈 2025-01-12 11:31:03

您误解了您引用的帖子。您必须选择一个现有列并将其定义为主列。还可以通过将它们全部放入定义中来设置复合主键。在你的例子中,我认为一个基因有几个成熟的 microRNA,所以主键可能应该由 (gene_id,mature_miRNA) 对组成。由于表中没有更多字段,因此不需要 autoload=True 标志。

db_table = Table("miranda", metadata,
                 Column("gene_id", Integer, primary_key=True),
                 Column("mature_miRNA", Integer, primary_key=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 in autoload=True flag.

db_table = Table("miranda", metadata,
                 Column("gene_id", Integer, primary_key=True),
                 Column("mature_miRNA", Integer, primary_key=True))

I don't know the types of fields in your table, so change them appropriately if they are not integer.

故事和酒 2025-01-12 11:31:03

将 sqlite 数据库的列 id 更改为 rowid

原始

db_table = Table("miranda", metadata,
                 Column("id", Integer, primary_key=True),
                 autoload=True)

修改

db_table = Table("miranda", metadata,
                 Column("rowid", Integer, primary_key=True),
                 autoload=True)

Change Column id to rowid for sqlite database

original:

db_table = Table("miranda", metadata,
                 Column("id", Integer, primary_key=True),
                 autoload=True)

modified:

db_table = Table("miranda", metadata,
                 Column("rowid", Integer, primary_key=True),
                 autoload=True)
汐鸠 2025-01-12 11:31:03

首先确保 id 列存在,然后尝试大小写。

还可以为表设置两个主键:

Column("id", Integer, primary_key=True),
Column("secondColumn", Integer, primary_key=True)

但是,如果任何其他行与该行完全匹配,则可能会导致更新异常。您可能最好重新创建表并插入您自己的 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:

Column("id", Integer, primary_key=True),
Column("secondColumn", Integer, primary_key=True)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文