Bypass checking to database structure (metadata.create_all)

80 views
Skip to first unread message

Christian Démolis

unread,
Oct 1, 2009, 5:39:41 AM10/1/09
to sqlal...@googlegroups.com
Hi again,

Is there any way to avoid checking database structure during the metadata.create_all declaration's phase?
It can be good to check when we are in test phase but when we are in production and we are sure of our model, it can be good to bypass create_all checking to database.

King Simon-NFHD78

unread,
Oct 1, 2009, 5:50:36 AM10/1/09
to sqlal...@googlegroups.com

create_all (and drop_all) have a 'checkfirst' parameter that defaults to True. If you set it to False, SA won't check to see if your tables already exist before issuing the CREATE statements:

<http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/schema.html#sqlalchemy.schema.MetaData.create_all>

Hope that helps,

Simon

Christian Démolis

unread,
Oct 1, 2009, 6:25:10 AM10/1/09
to sqlal...@googlegroups.com
Thx Simon,

I tried Base.metadata.create_all(engine, checkfirst=False)
but it throws an error.
When checkfirst is True, the declaration works.
I don t understand...

Maybe orm needs additional information in declarative classes when checkfirst=False because orm doesn t look in database in this case?

These is the error :

Z:\>python Declaration.py
Le temps de chargement des modules SQL ALCHEMY 0.25
Le temps de dÚclaration SQL ALCHEMY 0.156000137329
Traceback (most recent call last):
  File "Declaration.py", line 1435, in <module>
    Base.metadata.create_all(engine, checkfirst=False)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\sche
ma.py", line 1796, in create_all
    bind.create(self, checkfirst=checkfirst, tables=tables)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 1129, in create
    self._run_visitor(self.dialect.schemagenerator, entity, connection=connectio
n, **kwargs)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 1158, in _run_visitor
    visitorcallable(self.dialect, conn, **kwargs).traverse(element)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\sql\
visitors.py", line 89, in traverse
    return traverse(obj, self.__traverse_options__, self._visitor_dict)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\sql\
visitors.py", line 200, in traverse
    return traverse_using(iterate(obj, opts), obj, visitors)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\sql\
visitors.py", line 194, in traverse_using
    meth(target)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\sql\
compiler.py", line 831, in visit_metadata
    self.traverse_single(table)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\sql\
visitors.py", line 79, in traverse_single
    return meth(obj)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\sql\
compiler.py", line 870, in visit_table
    self.execute()
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 1812, in execute
    return self.connection.execute(self.buffer.getvalue())
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 824, in execute
    return Connection.executors[c](self, object, multiparams, params)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 888, in _execute_text
    return self.__execute_context(context)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 896, in __execute_context
    self._cursor_execute(context.cursor, context.statement, context.parameters[0
], context=context)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 950, in _cursor_execute
    self._handle_dbapi_exception(e, statement, parameters, cursor, context)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.5.6-py2.5.egg\sqlalchemy\engi
ne\base.py", line 931, in _handle_dbapi_exception
    raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidat
ed=is_disconnect)
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1064, "Erreur de syntaxe pr
\xe8s de ' \n\tcp VARCHAR, \n\tlatitude VARCHAR, \n\tlongitude VARCHAR, \n\teloi
gn' \xe0 la ligne 3") '\nCREATE TABLE maps_ville (\n\t`IdVille` INTEGER NOT NULL
 AUTO_INCREMENT, \n\tnom VARCHAR, \n\tcp VARCHAR, \n\tlatitude VARCHAR, \n\tlong
itude VARCHAR, \n\teloignement VARCHAR, \n\turl VARCHAR, \n\tPRIMARY KEY (`IdVil
le`)\n)\n\n' ()



2009/10/1 King Simon-NFHD78 <simon...@motorola.com>

limodou

unread,
Oct 1, 2009, 6:54:04 AM10/1/09
to sqlal...@googlegroups.com

It seems that there is no length for VARCHAR, the right syntax should
be VARCHAR(length), and I also need this problem when I testing in
Mysql, but there is no problem in Sqlite.


--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: http://uliwebproject.appspot.com
My Blog: http://hi.baidu.com/limodou

Christian Démolis

unread,
Oct 1, 2009, 8:13:20 AM10/1/09
to sqlal...@googlegroups.com
Ok, i just realize that create_all is useless when database already exist.
Starting my application is 6 seconds faster now.

Thanks all

2009/10/1 limodou <lim...@gmail.com>

Martijn Moeling

unread,
Apr 12, 2010, 5:20:37 AM4/12/10
to sqlal...@googlegroups.com
Hi,

I have a weird problem.

(Mysql)

When I  do a create_all(), i get the error : 

  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/engine/base.py", line 931, in _handle_dbapi_exception
    raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
sqlalchemy.exc.OperationalError: (OperationalError) (1050, "Table 'calendarevents' already exists") '\nCREATE TABLE `CalendarEvents` (\n\t`Id` INTEGER NOT NULL AUTO_INCREMENT, \n\t`CalendarId` INTEGER, \n\t`Allday` BOOL, \n\t`DtStart` DATETIME, \n\t`DtEnd` DATETIME, \n\t`DTStamp` DATETIME, \n\t`Class` VARCHAR(20), \n\t`Created` DATETIME, \n\t`Description` TEXT, \n\t`Duration` DATETIME, \n\t`LastModified` DATETIME, \n\t`Location` TEXT, \n\t`Priority` VARCHAR(10), \n\t`RecurId` DATETIME, \n\t`Sequence` INTEGER, \n\t`Status` TEXT, \n\t`Summary` TEXT, \n\t`Transparent` VARCHAR(15), \n\tuid TEXT, \n\turl TEXT, \n\t`Organizer` VARCHAR(100), \n\t`OrganizerCN` VARCHAR(100), \n\t`OrganizerDIR` VARCHAR(50), \n\t`OrganizerSendBy` VARCHAR(50), \n\t`OrganizerLanguage` VARCHAR(50), \n\tPRIMARY KEY (`Id`), \n\t FOREIGN KEY(`CalendarId`) REFERENCES `Calendars` (`Id`)\n)\n\n' ()

even though the table is not there!, Only the Calendars table is created

Any suggestions?

Martijn

Michael Bayer

unread,
Apr 12, 2010, 10:52:21 AM4/12/10
to sqlal...@googlegroups.com

seems likely that your MySQL database is on a case insensitive filesystem
so you should be naming your table "calendarevents", all lowercase.


>
> Martijn
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages