Discussion:
reflection failure with MySQL: Mapper could not assemble any primary key columns for mapped table
Felix Schwarz
2013-03-11 13:59:20 UTC
Permalink
Hey,

I'm trying to use reflection with SQLAlchemy 0.8 but I always get this exception:
sqlalchemy.exc.ArgumentError: Mapper Mapper|links|links could not assemble any
primary key columns for mapped table 'links'

mysql> show fields from links;
+--------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------+------+-----+---------+-------+
| l_from | int(8) unsigned | NO | PRI | 0 | |
| l_to | int(8) unsigned | NO | PRI | 0 | |
+--------+-----------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

The class definition is pretty simple:

class links(Base):
__table__ = Table('links', Base.metadata, autoload=True)

Traceback:
File "…/main.py", line 36, in connect_to_db
class links(Base):
File "…/sqlalchemy/ext/declarative.py", line 1343, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "…/sqlalchemy/ext/declarative.py", line 1336, in _as_declarative
**mapper_args)
File "…/sqlalchemy/orm/__init__.py", line 1129, in mapper
return Mapper(class_, local_table, *args, **params)
File "…/sqlalchemy/orm/mapper.py", line 203, in __init__
self._configure_pks()
File "…/sqlalchemy/orm/mapper.py", line 773, in _configure_pks
(self, self.mapped_table.description))

Any idea why this happens and how I fix the problem? (without having to
specify the ORM mapping myself)

fs
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Michael Bayer
2013-03-11 14:12:50 UTC
Permalink
X-Received: by 10.49.116.165 with SMTP id jx5mr781340qeb.22.1363011182430;
Mon, 11 Mar 2013 07:13:02 -0700 (PDT)
X-BeenThere: ***@googlegroups.com
Received: by 10.49.120.1 with SMTP id ky1ls964171qeb.3.gmail; Mon, 11 Mar 2013
07:12:53 -0700 (PDT)
X-Received: by 10.58.40.51 with SMTP id u19mr3503104vek.16.1363011172960;
Mon, 11 Mar 2013 07:12:52 -0700 (PDT)
Received: from smtp.pobox.com (b-pb-sasl-quonix.pobox.com. [208.72.237.35])
by gmr-mx.google.com with ESMTP id t20si1014383vdf.0.2013.03.11.07.12.52;
Mon, 11 Mar 2013 07:12:52 -0700 (PDT)
Received-SPF: neutral (google.com: 208.72.237.35 is neither permitted nor denied by best guess record for domain of ***@zzzcomputing.com) client-ip 8.72.237.35;
Received: from smtp.pobox.com (unknown [127.0.0.1])
by b-sasl-quonix.pobox.com (Postfix) with ESMTP id 5941BB41B
for <***@googlegroups.com>; Mon, 11 Mar 2013 10:12:51 -0400 (EDT)
Received: from b-pb-sasl-quonix.pobox.com (unknown [127.0.0.1])
by b-sasl-quonix.pobox.com (Postfix) with ESMTP id 4E37CB41A
for <***@googlegroups.com>; Mon, 11 Mar 2013 10:12:51 -0400 (EDT)
Received: from [192.168.1.109] (unknown [173.3.207.254]) (using TLSv1 with
cipher AES128-SHA (128/128 bits)) (No client certificate requested) by
b-sasl-quonix.pobox.com (Postfix) with ESMTPSA id E1A35B418 for
<***@googlegroups.com>; Mon, 11 Mar 2013 10:12:50 -0400 (EDT)
In-Reply-To: <***@oss.schwarz.eu>
X-Mailer: Apple Mail (2.1499)
X-Pobox-Relay-ID: C2332E7C-8A55-11E2-BDC6-26A52E706CDE-02456075!b-pb-sasl-quonix.pobox.com
X-Original-Sender: ***@zzzcomputing.com
X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral
(google.com: 208.72.237.35 is neither permitted nor denied by best guess
record for domain of ***@zzzcomputing.com) smtp.mail=***@zzzcomputing.com;
dkim=pass header.i=@pobox.com
Precedence: list
Mailing-list: list ***@googlegroups.com; contact sqlalchemy+***@googlegroups.com
List-ID: <sqlalchemy.googlegroups.com>
X-Google-Group-Id: 564602266895
List-Post: <http://groups.google.com/group/sqlalchemy/post?hl=en_US>, <mailto:***@googlegroups.com>
List-Help: <http://groups.google.com/support/?hl=en_US>, <mailto:sqlalchemy+***@googlegroups.com>
List-Archive: <http://groups.google.com/group/sqlalchemy?hl=en_US>
Sender: ***@googlegroups.com
List-Subscribe: <http://groups.google.com/group/sqlalchemy/subscribe?hl=en_US>,
<mailto:sqlalchemy+***@googlegroups.com>
List-Unsubscribe: <http://groups.google.com/group/sqlalchemy/subscribe?hl=en_US>,
<mailto:googlegroups-manage+564602266895+***@googlegroups.com>
Archived-At: <http://permalink.gmane.org/gmane.comp.python.sqlalchemy.user/33990>

can you send the "SHOW CREATE TABLE", I'll copy it exactly
Post by Felix Schwarz
Hey,
sqlalchemy.exc.ArgumentError: Mapper Mapper|links|links could not assemble any
primary key columns for mapped table 'links'
mysql> show fields from links;
+--------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------+------+-----+---------+-------+
| l_from | int(8) unsigned | NO | PRI | 0 | |
| l_to | int(8) unsigned | NO | PRI | 0 | |
+--------+-----------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
__table__ = Table('links', Base.metadata, autoload=True)
File "…/main.py", line 36, in connect_to_db
File "…/sqlalchemy/ext/declarative.py", line 1343, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "…/sqlalchemy/ext/declarative.py", line 1336, in _as_declarative
**mapper_args)
File "…/sqlalchemy/orm/__init__.py", line 1129, in mapper
return Mapper(class_, local_table, *args, **params)
File "…/sqlalchemy/orm/mapper.py", line 203, in __init__
self._configure_pks()
File "…/sqlalchemy/orm/mapper.py", line 773, in _configure_pks
(self, self.mapped_table.description))
Any idea why this happens and how I fix the problem? (without having to
specify the ORM mapping myself)
fs
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Felix Schwarz
2013-03-11 15:14:12 UTC
Permalink
Post by Michael Bayer
can you send the "SHOW CREATE TABLE", I'll copy it exactly
CREATE TABLE `links` (
`l_from` int(8) unsigned NOT NULL DEFAULT '0',
`l_to` int(8) unsigned NOT NULL DEFAULT '0',
UNIQUE KEY `l_from` (`l_from`,`l_to`),
KEY `l_to` (`l_to`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1


(sorry for breaking the threading, due to some misconfiguration I saw
Michael's mail only in the web interface.)
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Michael Bayer
2013-03-11 16:27:55 UTC
Permalink
OK well that table has no primary key established.

If I create a table with a PK:

create table test (id integer primary key);

you see PRIMARY KEY in the output:

mysql> show create table test;
+-------+----------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

so in this case you'd need to specify PK cols in your Table or in your mapper def.
Post by Felix Schwarz
Post by Michael Bayer
can you send the "SHOW CREATE TABLE", I'll copy it exactly
CREATE TABLE `links` (
`l_from` int(8) unsigned NOT NULL DEFAULT '0',
`l_to` int(8) unsigned NOT NULL DEFAULT '0',
UNIQUE KEY `l_from` (`l_from`,`l_to`),
KEY `l_to` (`l_to`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
(sorry for breaking the threading, due to some misconfiguration I saw
Michael's mail only in the web interface.)
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Felix Schwarz
2013-03-12 10:32:36 UTC
Permalink
Post by Michael Bayer
OK well that table has no primary key established.
I see. So even if MySQL tells me (in 'show fields') that a column is a primary
key, SQLAlchemy won't recognize it unless the column is explicitely marked as
primary key (as opposed to a unique key).

Makes sense, still a pitty that my DB schema is so broken (it's an old
MediaWiki dump).

fs
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Mauricio de Abreu Antunes
2013-03-12 12:13:19 UTC
Permalink
Yes, you need to tell SQLAlchemy that key is a primary key.
Post by Felix Schwarz
Post by Michael Bayer
OK well that table has no primary key established.
I see. So even if MySQL tells me (in 'show fields') that a column is a primary
key, SQLAlchemy won't recognize it unless the column is explicitely marked as
primary key (as opposed to a unique key).
Makes sense, still a pitty that my DB schema is so broken (it's an old
MediaWiki dump).
fs
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
*Mauricio de Abreu Antunes*
Mobile: (51)930-74-525
Skype: mauricio.abreua
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Michael Bayer
2013-03-12 14:43:23 UTC
Permalink
X-Received: by 10.49.24.194 with SMTP id w2mr1193129qef.42.1363099411131;
Tue, 12 Mar 2013 07:43:31 -0700 (PDT)
X-BeenThere: ***@googlegroups.com
Received: by 10.49.25.83 with SMTP id a19ls1187177qeg.33.gmail; Tue, 12 Mar
2013 07:43:25 -0700 (PDT)
X-Received: by 10.58.127.168 with SMTP id nh8mr4431361veb.27.1363099405592;
Tue, 12 Mar 2013 07:43:25 -0700 (PDT)
Received: from smtp.pobox.com (b-pb-sasl-quonix.pobox.com. [208.72.237.35])
by gmr-mx.google.com with ESMTP id cr9si1296927vdb.2.2013.03.12.07.43.25;
Tue, 12 Mar 2013 07:43:25 -0700 (PDT)
Received-SPF: neutral (google.com: 208.72.237.35 is neither permitted nor denied by best guess record for domain of ***@zzzcomputing.com) client-ip 8.72.237.35;
Received: from smtp.pobox.com (unknown [127.0.0.1])
by b-sasl-quonix.pobox.com (Postfix) with ESMTP id 4829CB071
for <***@googlegroups.com>; Tue, 12 Mar 2013 10:43:25 -0400 (EDT)
Received: from b-pb-sasl-quonix.pobox.com (unknown [127.0.0.1])
by b-sasl-quonix.pobox.com (Postfix) with ESMTP id 3E319B070
for <***@googlegroups.com>; Tue, 12 Mar 2013 10:43:25 -0400 (EDT)
Received: from [192.168.1.109] (unknown [173.3.207.254]) (using TLSv1 with
cipher AES128-SHA (128/128 bits)) (No client certificate requested) by
b-sasl-quonix.pobox.com (Postfix) with ESMTPSA id D4834B06F for
<***@googlegroups.com>; Tue, 12 Mar 2013 10:43:24 -0400 (EDT)
In-Reply-To: <***@oss.schwarz.eu>
X-Mailer: Apple Mail (2.1499)
X-Pobox-Relay-ID: 31BBBAAC-8B23-11E2-B878-26A52E706CDE-02456075!b-pb-sasl-quonix.pobox.com
X-Original-Sender: ***@zzzcomputing.com
X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral
(google.com: 208.72.237.35 is neither permitted nor denied by best guess
record for domain of ***@zzzcomputing.com) smtp.mail=***@zzzcomputing.com;
dkim=pass header.i=@pobox.com
Precedence: list
Mailing-list: list ***@googlegroups.com; contact sqlalchemy+***@googlegroups.com
List-ID: <sqlalchemy.googlegroups.com>
X-Google-Group-Id: 564602266895
List-Post: <http://groups.google.com/group/sqlalchemy/post?hl=en_US>, <mailto:***@googlegroups.com>
List-Help: <http://groups.google.com/support/?hl=en_US>, <mailto:sqlalchemy+***@googlegroups.com>
List-Archive: <http://groups.google.com/group/sqlalchemy?hl=en_US>
Sender: ***@googlegroups.com
List-Subscribe: <http://groups.google.com/group/sqlalchemy/subscribe?hl=en_US>,
<mailto:sqlalchemy+***@googlegroups.com>
List-Unsubscribe: <http://groups.google.com/group/sqlalchemy/subscribe?hl=en_US>,
<mailto:googlegroups-manage+564602266895+***@googlegroups.com>
Archived-At: <http://permalink.gmane.org/gmane.comp.python.sqlalchemy.user/34007>
Post by Felix Schwarz
Post by Michael Bayer
OK well that table has no primary key established.
I see. So even if MySQL tells me (in 'show fields') that a column is a primary
key, SQLAlchemy won't recognize it unless the column is explicitely marked as
primary key (as opposed to a unique key).
Makes sense, still a pitty that my DB schema is so broken (it's an old
MediaWiki dump).
a unique key is not the same thing as a primary key, and MySQL reflection uses the SHOW CREATE TABLE output to reflect, as we can't get all the information we need in any other way for that platform (one of the many ways MySQL is unlike any other backend).

I suppose the reflection could have some feature where you flag it as, "single unique key w/no primary key means that's the primary key", but we've not had requests for this feature before.

We will at some point be adding a feature whereby one can intercept the event where we reflect the primary key; we have this event for individual columns now. When that event is present, you'll be able to flip on the "primary key" flag for unique column sets as needed.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Felix Schwarz
2013-03-13 10:11:09 UTC
Permalink
Post by Michael Bayer
I suppose the reflection could have some feature where you flag it as,
"single unique key w/no primary key means that's the primary key", but
we've not had requests for this feature before.
We will at some point be adding a feature whereby one can intercept the
event where we reflect the primary key; we have this event for individual
columns now. When that event is present, you'll be able to flip on the
"primary key" flag for unique column sets as needed.
That sounds very helpful - looking forward to every new version of SQLAlchemy :-)

fs
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...