[sqlalchemy] Re: does tometadata have to change foreign key schema name if it already specified?
Thank you, Michael, it was very helpful! Michael Bayer написав: > this can be changed but the function still makes no sense. What if > you also said "t3 = users.tometadata(metadata, > schema='SOME_SCHEMA')" ? then you would want the "system.users.id" > FK to be changed.the tometadata() approach doesn't provide an API > that can take the use case of variable schema names into account. > > Therefore, feel free to implement tometadata() yourself using > column.copy() and constraint.copy(): > > def copy_table(table, metadata, schema_map): > args = [] > for c in table.columns: > args.append(c.copy()) > for c in table.constraints: > if isinstance(c, ForeignKeyConstraint): > elem = list(c.elements)[0] > schema = schema_map[elem.column.table.schema] > else: > schema=None > args.append(c.copy(schema=schema)) > return Table(table.name, metadata, > schema=schema_map[table.schema], *args) > > usage: > > m2 = MetaData() > t2 = copy_table(t, m2, {None:'SOME_SCHEMA', 'system':'system'}) > > will map tables with no schema to SOME_SCHEMA, tables with "system" to > the "system" schema. > > > On Jan 20, 2009, at 3:02 PM, sector119 wrote: > > > > > Hi ALL! > > > > Does tometadata have to change foreign key schema name if it already > > specified? > > For example if I have column 'user_id' with 'system.users.id' FK - > > tometadata change 'system' schema to SOME_SCHEMA. Is it ok, or > > tometadata have to set schema to SOME_SCHEMA for street_id, > > locality_id columns _only_ ? > > > > transactions_t = Table('transactions', meta.metadata, > > Column('id', Integer, > > primary_key=True, autoincrement=False), > > Column('user_id', Integer, > > ForeignKey('system.users.id'), nullable=False), > > Column('street_id', Integer, > > ForeignKey('streets.id'), nullable=False), > > Column('locality_id', Integer, > > ForeignKey('locality.id'), nullable=False), > > Column('sum', Integer, > > nullable=False) > > ) > > > > transactions_t.tometadata(metadata, schema='SOME_SCHEMA').create() --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: does tometadata have to change foreign key schema name if it already specified?
this can be changed but the function still makes no sense. What if you also said "t3 = users.tometadata(metadata, schema='SOME_SCHEMA')" ? then you would want the "system.users.id" FK to be changed.the tometadata() approach doesn't provide an API that can take the use case of variable schema names into account. Therefore, feel free to implement tometadata() yourself using column.copy() and constraint.copy(): def copy_table(table, metadata, schema_map): args = [] for c in table.columns: args.append(c.copy()) for c in table.constraints: if isinstance(c, ForeignKeyConstraint): elem = list(c.elements)[0] schema = schema_map[elem.column.table.schema] else: schema=None args.append(c.copy(schema=schema)) return Table(table.name, metadata, schema=schema_map[table.schema], *args) usage: m2 = MetaData() t2 = copy_table(t, m2, {None:'SOME_SCHEMA', 'system':'system'}) will map tables with no schema to SOME_SCHEMA, tables with "system" to the "system" schema. On Jan 20, 2009, at 3:02 PM, sector119 wrote: > > Hi ALL! > > Does tometadata have to change foreign key schema name if it already > specified? > For example if I have column 'user_id' with 'system.users.id' FK - > tometadata change 'system' schema to SOME_SCHEMA. Is it ok, or > tometadata have to set schema to SOME_SCHEMA for street_id, > locality_id columns _only_ ? > > transactions_t = Table('transactions', meta.metadata, > Column('id', Integer, > primary_key=True, autoincrement=False), > Column('user_id', Integer, > ForeignKey('system.users.id'), nullable=False), > Column('street_id', Integer, > ForeignKey('streets.id'), nullable=False), > Column('locality_id', Integer, > ForeignKey('locality.id'), nullable=False), > Column('sum', Integer, > nullable=False) > ) > > transactions_t.tometadata(metadata, schema='SOME_SCHEMA').create() --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---