Hi Kevin, 
I realize this is an old thread, but if you're still around, I just wanted 
to confirm whether this script is capable of extracting and creating 
foreign-keys with the "reference" keyword?  I'm thinking not, since it did 
not give me any after I ran it, but perhaps there's an option I'm not aware 
of.

Thanks,
Peter

On Sunday, March 20, 2011 9:34:01 AM UTC-7, Kevin Ivarsen wrote:
>
> Hi folks,
>
> Today I made some enhancements to the 
> web2py/scripts/extract_mysql_models.py script that converts existing MySQL 
> tables into a DAL representation. I've attached a copy for anyone that 
> might find it useful.
>
> The major changes are as follows:
>  - I got rid of the dependencies on the external mysql and mysqldump 
> executables and now use MySQLdb directly. This simplifies the parsing a lot 
> and makes the tool a little more self-contained. (Linux users probably have 
> the mysql command line tools installed, but Windows users may not -- at 
> least not in the PATH)
>  - If your id column does not have the default "id" name, the script looks 
> for an AUTO INCREMENT column and marks that with type "id" instead.
>  - If you have a COMMENT specified in your MySQL table, it is added as a 
> Python comment to the Field line
>  - You are no longer restricted to localhost - you can read tables form 
> remote databases
>  - There are several new command line options, such as:
>    --dalname (specify the variable name used for the DAL; e.g. [var] = 
> DAL(...); [var].define_table(...))
>    --colcomments  (add a comment to the end of each Field line showing the 
> original column specification from CREATE TABLE... useful for nothing 
> things like default values, original datatype, etc.)
>    --commentalign (aligns comments to some number of spaces in the 
> generated text)
>    --notnull (detects NOT NULL and adds notnull=True to Field)
>    --singlemigrate (use a single migrate=False in DAL() rather than in 
> each define_table())
>    --verbose (adds logging to stderr so that if it crashes, you have a 
> chance of figuring out which table failed)
>
> You can see the full usage by running the script without any arguments.
>
>
> Example use:
>
> extract_mysql_models.py --user kevin --password secret --host 
> example.com--database mywiki --dalname wikidb --colcomments --singlemigrate > 
> mywiki.py
>
> Generated output (only looks pretty with fixed-width formatting!):
>
> wikidb = DAL('mysql://kevin:sec...@example.com/mywiki', migrate=False)
>
> wikidb.define_table('comment', 
>     Field('id', 'id'),                         # (`id` int(11) NOT NULL 
> AUTO_INCREMENT,)
>     Field('page_id', 'integer'),               # (`page_id` int(11) 
> DEFAULT NULL,)
>     Field('body', 'text'),                     # (`body` longtext,)
>     Field('created_on', 'datetime'),           # (`created_on` datetime 
> DEFAULT NULL,)
>     Field('created_by', 'integer'),            # (`created_by` int(11) 
> DEFAULT NULL,)
>     )
>
> wikidb.define_table('document', 
>     Field('id', 'id'),                         # (`id` int(11) NOT NULL 
> AUTO_INCREMENT,)
>     Field('page_id', 'integer'),               # (`page_id` int(11) 
> DEFAULT NULL,)
>     Field('name', 'string'),                   # (`name` varchar(255) 
> DEFAULT NULL,)
>     Field('file', 'string'),                   # (`file` varchar(255) 
> DEFAULT NULL,)
>     Field('created_on', 'datetime'),           # (`created_on` datetime 
> DEFAULT NULL,)
>     Field('created_by', 'integer'),            # (`created_by` int(11) 
> DEFAULT NULL,)
>     )
>
> wikidb.define_table('page', 
>     Field('id', 'id'),                         # (`id` int(11) NOT NULL 
> AUTO_INCREMENT,)
>     Field('body', 'text'),                     # (`body` longtext,)
>     Field('created_on', 'datetime'),           # (`created_on` datetime 
> DEFAULT NULL,)
>     Field('created_by', 'integer'),            # (`created_by` int(11) 
> DEFAULT NULL,)
>     Field('title', 'string'),                  # (`title` varchar(255) 
> DEFAULT NULL,)
>     )
>
>
> Massimo: feel free to integrate into the web2py source if you think it 
> would be useful.
>
> Cheers,
> Kevin
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to