Re: [sqlalchemy] Find whether a synonym points to a foreign key or a relationship (Looking for your blessing)
Thank you... Of course using all those descriptors for every attribute is a pretty java-esque Yeah... but you know... It's not easy getting rid of the past... And I'm pretty O.C.D, so I lve getters/setters... I'm opened to new experiences, though :-) Any hint, suggestion... whatever! you may have will be very appreciated... As usual, thank you so much! 2011/2/27 Michael Bayer mike...@zzzcomputing.com: On Feb 27, 2011, at 6:45 PM, Hector Blanco wrote: A few days ago I asked what appears in the body of the message, a few lines below. To summarize: Let's say I have a class User (yeah, to define users in my application) and each user can belong to one UserGroup (another class of my application). The User class would be something like: class User(declarativeBase): Represents a user __tablename__ = users _id = Column(id, Integer, primary_key=True) _firstName = Column(first_name, String(50)) _lastName = Column(last_name, String(50)) _userName = Column(user_name, String(50), unique=True, nullable=False) _password = Column(password, String(64), nullable=False) _userGroupId = Column(user_group_id, Integer, ForeignKey(user_groups.id)) _userGroup = relationship(UserGroup, uselist=False) id = synonym('_id', descriptor=property(getId, setId)) firstName = synonym('_firstName', descriptor=property(getFirstName, setFirstName)) lastName = synonym('_lastName', descriptor=property(getLastName, setLastName)) userName = synonym('_userName', descriptor=property(getUserName, setUserName)) password = synonym('_password', descriptor=property(getPassword, setPassword)) userGroupId = synonym('_userGroupId', descriptor=property(getUserGroupId, setUserGroupId)) userGroup = synonym('_userGroup', descriptor=property(getUserGroup, setUserGroup)) I wanted to find a way to find which synonyms pointed to foreign keys and which ones pointed to relationships. Basically, having a couple of methods like the following: def getRelationships(cls): that when invoked with getRelationships(User.User) would return a list with [userGroup] (withouth the _ in front) and another: def getForeignKeys(cls): that would return [userGroupId] So far I've done this: def getRelationships(cls): retval = list() mapper = sqlalchemy.orm.class_mapper(cls) actualNameToSynonym = dict() relationships = set() for prop in mapper.iterate_properties: if isinstance(prop, sqlalchemy.orm.properties.SynonymProperty): actualNameToSynonym[prop.name] = prop.key # dictionary _userName, userName, userGroup, _userGroup elif isinstance(prop, sqlalchemy.orm.properties.RelationshipProperty): relationships.add(prop.key) #set with _userGroup, and rest of relationships for relationship in relationships: retval.append(actualNameToSynonym[relationship]) return retval def getForeignKeys(cls): retval = list() mapper = sqlalchemy.orm.class_mapper(cls) actualNameToSynonym = dict() columnsWithForeignKeys = set() for prop in mapper.iterate_properties: if isinstance(prop, sqlalchemy.orm.properties.SynonymProperty): actualNameToSynonym[prop.name] = prop.key # dictionary _userName, userName, userGroup, _userGroup elif isinstance(prop, sqlalchemy.orm.properties.ColumnProperty): for column in prop.columns: if len(column.foreign_keys) 0: columnsWithForeignKeys.add(prop.key) for columnWithForeignKeys in columnsWithForeignKeys: retval.append(actualNameToSynonym[columnWithForeignKeys]) return retval Both are very similar: First they create a dictionary mapping the synonym's key with the real name (_userGroup, userGroup) and store the relationships or the columns that have a foreign key in a set (for the method that tries to get relationships, that set would be set(_userGroup) and for the one that tries to get foreign keys, set(_userGroupId)) . In a second for loop they match that underscored name with the name of the synonym to return a list with the names of the synonyms, and not the actual columns (basically, to transform _userGroupId to userGroupId) They seem to work, at least with my not-complicated-at-all classes, but I'd like to know what do you guys think of my approach. Is it good? Can it break something? Is there a better way? No thats a pretty OK way , there's an argument called resolve_synonyms to get_property() in 0.6 but that's gone away in 0.7 anyway. There
Re: [sqlalchemy] Find whether a synonym points to a foreign key or a relationship (Looking for your blessing)
A few days ago I asked what appears in the body of the message, a few lines below. To summarize: Let's say I have a class User (yeah, to define users in my application) and each user can belong to one UserGroup (another class of my application). The User class would be something like: class User(declarativeBase): Represents a user __tablename__ = users _id = Column(id, Integer, primary_key=True) _firstName = Column(first_name, String(50)) _lastName = Column(last_name, String(50)) _userName = Column(user_name, String(50), unique=True, nullable=False) _password = Column(password, String(64), nullable=False) _userGroupId = Column(user_group_id, Integer, ForeignKey(user_groups.id)) _userGroup = relationship(UserGroup, uselist=False) id = synonym('_id', descriptor=property(getId, setId)) firstName = synonym('_firstName', descriptor=property(getFirstName, setFirstName)) lastName = synonym('_lastName', descriptor=property(getLastName, setLastName)) userName = synonym('_userName', descriptor=property(getUserName, setUserName)) password = synonym('_password', descriptor=property(getPassword, setPassword)) userGroupId = synonym('_userGroupId', descriptor=property(getUserGroupId, setUserGroupId)) userGroup = synonym('_userGroup', descriptor=property(getUserGroup, setUserGroup)) I wanted to find a way to find which synonyms pointed to foreign keys and which ones pointed to relationships. Basically, having a couple of methods like the following: def getRelationships(cls): that when invoked with getRelationships(User.User) would return a list with [userGroup] (withouth the _ in front) and another: def getForeignKeys(cls): that would return [userGroupId] So far I've done this: def getRelationships(cls): retval = list() mapper = sqlalchemy.orm.class_mapper(cls) actualNameToSynonym = dict() relationships = set() for prop in mapper.iterate_properties: if isinstance(prop, sqlalchemy.orm.properties.SynonymProperty): actualNameToSynonym[prop.name] = prop.key # dictionary _userName, userName, userGroup, _userGroup elif isinstance(prop, sqlalchemy.orm.properties.RelationshipProperty): relationships.add(prop.key) #set with _userGroup, and rest of relationships for relationship in relationships: retval.append(actualNameToSynonym[relationship]) return retval def getForeignKeys(cls): retval = list() mapper = sqlalchemy.orm.class_mapper(cls) actualNameToSynonym = dict() columnsWithForeignKeys = set() for prop in mapper.iterate_properties: if isinstance(prop, sqlalchemy.orm.properties.SynonymProperty): actualNameToSynonym[prop.name] = prop.key # dictionary _userName, userName, userGroup, _userGroup elif isinstance(prop, sqlalchemy.orm.properties.ColumnProperty): for column in prop.columns: if len(column.foreign_keys) 0: columnsWithForeignKeys.add(prop.key) for columnWithForeignKeys in columnsWithForeignKeys: retval.append(actualNameToSynonym[columnWithForeignKeys]) return retval Both are very similar: First they create a dictionary mapping the synonym's key with the real name (_userGroup, userGroup) and store the relationships or the columns that have a foreign key in a set (for the method that tries to get relationships, that set would be set(_userGroup) and for the one that tries to get foreign keys, set(_userGroupId)) . In a second for loop they match that underscored name with the name of the synonym to return a list with the names of the synonyms, and not the actual columns (basically, to transform _userGroupId to userGroupId) They seem to work, at least with my not-complicated-at-all classes, but I'd like to know what do you guys think of my approach. Is it good? Can it break something? Is there a better way? Thank you! 2011/2/18 Hector Blanco white.li...@gmail.com: I'll give it a try!! Thank you! 2011/2/18 Michael Bayer mike...@zzzcomputing.com: On Feb 17, 2011, at 6:37 PM, Hector Blanco wrote: Hello everyone! Let's say I have a class defined like this: class User(declarativeBase): Represents a user __tablename__ = users _id = Column(id, Integer, primary_key=True) _phone = Column(phone, String(16)) _userName = Column(user_name, String(50), unique=True, nullable=False) _password = Column(password, String(64), nullable=False) _userGroupId
Re: [sqlalchemy] Find whether a synonym points to a foreign key or a relationship (Looking for your blessing)
On Feb 27, 2011, at 6:45 PM, Hector Blanco wrote: A few days ago I asked what appears in the body of the message, a few lines below. To summarize: Let's say I have a class User (yeah, to define users in my application) and each user can belong to one UserGroup (another class of my application). The User class would be something like: class User(declarativeBase): Represents a user __tablename__ = users _id = Column(id, Integer, primary_key=True) _firstName = Column(first_name, String(50)) _lastName = Column(last_name, String(50)) _userName = Column(user_name, String(50), unique=True, nullable=False) _password = Column(password, String(64), nullable=False) _userGroupId = Column(user_group_id, Integer, ForeignKey(user_groups.id)) _userGroup = relationship(UserGroup, uselist=False) id = synonym('_id', descriptor=property(getId, setId)) firstName = synonym('_firstName', descriptor=property(getFirstName, setFirstName)) lastName = synonym('_lastName', descriptor=property(getLastName, setLastName)) userName = synonym('_userName', descriptor=property(getUserName, setUserName)) password = synonym('_password', descriptor=property(getPassword, setPassword)) userGroupId = synonym('_userGroupId', descriptor=property(getUserGroupId, setUserGroupId)) userGroup = synonym('_userGroup', descriptor=property(getUserGroup, setUserGroup)) I wanted to find a way to find which synonyms pointed to foreign keys and which ones pointed to relationships. Basically, having a couple of methods like the following: def getRelationships(cls): that when invoked with getRelationships(User.User) would return a list with [userGroup] (withouth the _ in front) and another: def getForeignKeys(cls): that would return [userGroupId] So far I've done this: def getRelationships(cls): retval = list() mapper = sqlalchemy.orm.class_mapper(cls) actualNameToSynonym = dict() relationships = set() for prop in mapper.iterate_properties: if isinstance(prop, sqlalchemy.orm.properties.SynonymProperty): actualNameToSynonym[prop.name] = prop.key # dictionary _userName, userName, userGroup, _userGroup elif isinstance(prop, sqlalchemy.orm.properties.RelationshipProperty): relationships.add(prop.key) #set with _userGroup, and rest of relationships for relationship in relationships: retval.append(actualNameToSynonym[relationship]) return retval def getForeignKeys(cls): retval = list() mapper = sqlalchemy.orm.class_mapper(cls) actualNameToSynonym = dict() columnsWithForeignKeys = set() for prop in mapper.iterate_properties: if isinstance(prop, sqlalchemy.orm.properties.SynonymProperty): actualNameToSynonym[prop.name] = prop.key # dictionary _userName, userName, userGroup, _userGroup elif isinstance(prop, sqlalchemy.orm.properties.ColumnProperty): for column in prop.columns: if len(column.foreign_keys) 0: columnsWithForeignKeys.add(prop.key) for columnWithForeignKeys in columnsWithForeignKeys: retval.append(actualNameToSynonym[columnWithForeignKeys]) return retval Both are very similar: First they create a dictionary mapping the synonym's key with the real name (_userGroup, userGroup) and store the relationships or the columns that have a foreign key in a set (for the method that tries to get relationships, that set would be set(_userGroup) and for the one that tries to get foreign keys, set(_userGroupId)) . In a second for loop they match that underscored name with the name of the synonym to return a list with the names of the synonyms, and not the actual columns (basically, to transform _userGroupId to userGroupId) They seem to work, at least with my not-complicated-at-all classes, but I'd like to know what do you guys think of my approach. Is it good? Can it break something? Is there a better way? No thats a pretty OK way , there's an argument called resolve_synonyms to get_property() in 0.6 but that's gone away in 0.7 anyway. There aren't too many choices for types of *Property. Perhaps if you wanted to handle CompositeProperty, though you wouldn't need to do so in 0.7 since composite doesn't replace the column props in 0.7. Of course using all those descriptors for every attribute is a pretty java-esque way of going about things but that's a different issue. Thank you! 2011/2/18 Hector Blanco