On Sep 22, 2007, at 5:17 PM, Frederik wrote:

>
> Hi!
>
> I have an XML file that provides the information needed for table
> creation (column names, types, etc.). Is there any easy way in
> SQLAlchemy to dynamically build Table objects from this information?
> So if I get something like
>
> [{'name': 'firstname', 'type': 'string', 'length': 40}, {'name':
> 'lastname', 'type': 'string', 'length': 40}]
>
> from the XML file, I want something like
>
> mytable = Table('mytable', metadata,
>     Column('firstname', types.String(40)),
>     Column('lastname', types.String(40))
> )
>
> to be generated.
> (oversimplified, I omitted stuff like primary_key, etc.)
>
> A simple way would be to generate a code string from the information
> and execute it with exec, but I'd like to know if there's a better way
> to do this in SQLAlchemy.

Table is a class constructor, the Column entries are *args sent to  
the constructor.  just create Column objects on the fly....i.e.

types = {'string':String, 'int':Integer}

res = []
for rec in xmlrecords:
        res.append(Column(rec['name'], types[rec['type']], ...))

t = Table(*res)

build on this as necessary to get the type arguments in, primary key  
flags, etc.

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to