Skip to content

Registration of class to declarative base with module name #2338

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot
Collaborator

Migrated issue, originally created by Anonymous

At the moment classes which inherit Base are registered with just their classname. When multiple classes with the same name inherit Base a warning will appear:

SAWarning: The classname 'Class' is already in the registry of this declarative base, mapped to <class 'path.to.Class'>

If the class was registered including the module name, so 'path.to.Class', then it would be possible to uses classes with the same name. Of course as long as they obey the Python rules concerning namespaces.

Example:

When a relationship is defined a string name can be used. When only one class with this name is registered, this class can be used. If multiple classes have the same name, the path to the class can be defined in the string name.

Let's say we have the following relationship:

relation = relationship("Class")

If only one class with the name "Class" is registered with the declarative base, this class can be used. If multiple classes with the name "Class" are found, the path should be defined:

relation = relationship("path.to.Class")

When multiple classes with the same name are found and the relation defines only the class name, an error can be raised.

Multiple classes with the classname "Class" are in the registry of this declarative base. Please including the module name.

It would even be possible to list the classes with module names so the user can pick the right one.

Current workarounds:

  • Don't use classes with the same name with the same declarative base
  • Use multiple declarative bases
  • Import the class you want a relation to and refer to this. (This is not always possible)
  • Only define the relations after both classes have been declared. (This will still give the SAWarnings.)

Activity

sqlalchemy-bot

sqlalchemy-bot commented on Dec 1, 2011

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

I like this though I wonder about namespace collisions. We use eval(code) with a dict passed to locals that will look into the named registry when a key is otherwise missing. So a class that has "path.to.Class" would need to put tokens for "path" in the registry, as well as "path" would need to be either the actual "path" module, or some further namespaced object that then has "to" on it. The presence of many new names, i.e. for every base module, in the registry might produce other problems...or maybe not.

in any case the system where this happens starts at http://www.sqlalchemy.org/trac/browser/lib/sqlalchemy/ext/declarative.py#L1299, I guess we'd add a new "class _GetModule" to handle this new level of lookup.

sqlalchemy-bot

sqlalchemy-bot commented on Dec 1, 2011

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • added labels: declarative
  • set milestone to "0.7.5"
  • changed assignee from "zzzeek" to "chrisw"
sqlalchemy-bot

sqlalchemy-bot commented on Dec 1, 2011

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed assignee from "chrisw" to "zzzeek"
sqlalchemy-bot

sqlalchemy-bot commented on Jan 22, 2012

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

this isn't high priority and has the potential to be destabilizing so pushing to 0.8 for now.

sqlalchemy-bot

sqlalchemy-bot commented on Jan 22, 2012

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed milestone from "0.7.5" to "0.8.0"
sqlalchemy-bot

sqlalchemy-bot commented on Aug 5, 2012

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

funny thing, how in #2526 we changed decl_class_registry to be weak referencing on the value side. So this becomes a lot more fun, in that we are severely limited what we can place in this dictionary for values. the "multiple class names" error suggests we need to store a value other than a class for a name with multiple hits.

sqlalchemy-bot

sqlalchemy-bot commented on Aug 5, 2012

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

6bd4694

sqlalchemy-bot

sqlalchemy-bot commented on Aug 5, 2012

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
sqlalchemy-bot

sqlalchemy-bot commented on Oct 28, 2012

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

partial paths now supported in 63a9d80

added
declarativehas to do with the declarative API, scanning classes and mixins for attributes to be mapped
on Nov 27, 2018
added this to the 0.8.0b1 milestone on Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    declarativehas to do with the declarative API, scanning classes and mixins for attributes to be mappedfeature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sqlalchemy-bot

        Issue actions

          Registration of class to declarative base with module name · Issue #2338 · sqlalchemy/sqlalchemy