ORM and table with no primary key

490 views
Skip to first unread message

Seth P

unread,
Oct 17, 2016, 3:36:24 PM10/17/16
to sqlalchemy
I realize that the orm really wants/needs a table to have a primary key: http://docs.sqlalchemy.org/en/rel_1_1/faq/ormconfiguration.html?#how-do-i-map-a-table-that-has-no-primary-key

Alas I have to deal with an existing table with no primary key. That said, it does have a unique constraint on a subset of the columns, though some of them are nullable (and indeed contain nulls). I've set up a declarative model with these columns labeled as primary_key=True, and things seem to work... until they don't, e.g. when calling delete() on some objects followed by flush():
sqlalchemy.orm.exc.FlushError: Can't delete from table MY_SCHEMA.my_table using NULL for primary key value on column my_table.MY_COLUMN_1

I know this is a long shot, but is there any way to tell the orm not to be such a stickler for detail, and just go ahead and emit the sql? I dread not being able to use the orm...

Mike Bayer

unread,
Oct 17, 2016, 3:58:36 PM10/17/16
to sqlal...@googlegroups.com
that error is there right now because we don't emit the "col IS NULL"
SQL within that section of the persistence code.

took me a long time to find the history on this because I thought it had
been discussed but looks like the answer was "no immediate plans":
https://bitbucket.org/zzzeek/sqlalchemy/issues/2127#comment-9011753

that is, we can't just drop that assertion without adding logic to alter
the SQL for some parameters.



>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Seth P

unread,
Oct 17, 2016, 4:11:17 PM10/17/16
to sqlalchemy
On Monday, October 17, 2016 at 3:58:36 PM UTC-4, Mike Bayer wrote:
that error is there right now because we don't emit the "col IS NULL"
SQL within that section of the persistence code.

took me a long time to find the history on this because I thought it had
been discussed but looks like the answer was "no immediate plans":
https://bitbucket.org/zzzeek/sqlalchemy/issues/2127#comment-9011753

that is, we can't just drop that assertion without adding logic to alter
the SQL for some parameters.
 
I see. Makes sense. Thanks.
Reply all
Reply to author
Forward
0 new messages