0

A web2py and python newbie here.

I tried to do a sample web app with MySQL DB as backend DB. I have created a table in MySQL separately and populated with values and I want to display the table values in the UI. I have an error -

"class 'gluon.contrib.pymysql.err.InternalError'> (1050, u"Table '' already exists")"

My config files are below:

db.py

if not request.env.web2py_runtime_gae:
    db = DAL('mysql://xxxxx',pool_size=1,check_reserved=['all'])
else:

    session.connect(request, response, db=db)

response.generic_patterns = ['*'] if request.is_local else []


from gluon.tools import Auth, Crud, Service, PluginManager, prettydate
auth = Auth(db)
crud, service, plugins = Crud(db), Service(), PluginManager()

## create all tables needed by auth if not custom tables
auth.define_tables(username=False, signature=False)


## after defining tables, uncomment below to enable auditing
# auth.enable_record_versioning(db)
db.define_table('user_details',
   Field('user_id', 'text'),
    Field('first_name', 'text'),
    Field('last_name', 'text'),
    Field('city', 'text'),
    Field('user_st', 'text'),migrate=True)

My home page look like this

{{ rows = db(db.user_details).select() }}
{{if len(rows):}}
<ul>
{{ for r in rows: }}
  <li>

        {{=r.name}}

  </li>
{{pass}}
</ul>
{{pass}}

I am not sure what I am missing. Any help appreciated, thanks.


I got it now. Just had to change migrate=False. Thanks.

4

1 回答 1

1

您有migrate=True,并且 web2py 没有任何记录表明它已经创建了表,因此它正在尝试再次创建它。fake_migrate=True您可以通过临时设置(或者,设置migrate=False并且不让 web2py 处理迁移)来让 web2py 更新它关于表的记录。

另外,请注意,默认情况下,web2py 期望每个表都包含一个名为“id”的自增整数字段,因此您应该确保您的表包含这样一个字段。更好的是,如果表是新的,不要在 MySQL 中手动创建它。相反,只需让 web2py 创建它(它会在第一次运行表定义时创建它)。一旦它由 web2py 创建,您就可以添加任何您喜欢的记录。

于 2013-10-18T20:39:14.117 回答