在WEB2py中引用复合主键

时间:2018-04-15 18:15:56

标签: reference foreign-keys primary-key web2py composite

我试图在web2py中引用复合主键。 我在桌面集会中获得了2个主键,而#34; tanda"这是一个反对"反弹"的弱实体,从反弹中获得了两个主要关键属性。问题是我不能很好地使用外键。

     # -*- coding: utf-8 -*-

     from gluon.tools import Auth
     #autentificacio
     from gluon.tools import AuthJWT
     #jeson web tokens

     db = DAL("sqlite://storage.sqlite")

     auth = Auth(db, jwt = {'secret_key':'secret_key_tfg_gps'})
     auth.settings.registration_requires_verification=False
     auth.settings.registration_requires_approval=False

     #auth = Auth(db)
     #myjwt = AuthJWT(auth, secret_key='secret_key_tfg_gps')

     auth.define_tables(username=True,signature=False)

     """ 
     SQLITE3 user table

     CREATE TABLE user(
          user_nickname TEXT NOT NULL,
          user_email TEXT NOT NULL UNIQUE,
          user_pass  TEXT NOT NULL,
          user_real_name TEXT,
          user_surname TEXT,
          user_tlf TEXT,
          user_escuderia TEXT,
          PRIMARY KEY (user_nickname));
          """
         db.define_table('user',
             Field('user_nickname',notnull=True),
             Field('user_email',unique=True,notnull=True),
             Field('user_pass',notnull=True),
             Field('user_real_name'),
             Field('user_surname'),
             Field('user_tlf'),
             Field('user_escuderia'),
             primarykey=['user_nickname'])


         """
        SQLITE 3 RALLY_TABLE:

        CREATE TABLE rally(
        rally_nom TEXT NOT NULL,
        rally_edicio INTEGER NOT NULL,
        rally_superficie TEXT,
        rally_visibilitat TEXT,
        PRIMARY KEY(rally_nom,rally_edicio));

        """

        db.define_table('rally',
            Field('rally_nom',notnull=True),
            Field('rally_edicio','integer',notnull=True),
            Field('rally_superficie'),
            Field('rally_visibilitat'),
            primarykey=['rally_nom','rally_edicio'],
            format = '%(rally_nom)s %(rally_edicio)s')

         """
         SQLITE3: TANDA_TABLE
         tanda_nom TEXT NOT NULL,
         tanda_rally TEXT NOT NULL,
         tanda_edicio INTEGER NOT NULL,
         tanda_horari_parc_tancat INTEGER,
         tanda_maximinscrits INTEGER,
         PRIMARY KEY(tanda_nom,tanda_rally,tanda_edicio),
         FOREIGN KEY (tanda_rally,tanda_edicio) REFERENCES rally(rally_nom,rally_edicio) ON UPDATE CASCADE,
         FOREIGN KEY (tanda_rally,tanda_edicio) REFERENCES rally(rally_nom,rally_edicio) ON DELETE CASCADE);
         """

         db.define_table('tanda',
         Field('tanda_nom'),
         Field('tanda_rally','reference rally.rally_nom'),
         Field('tanda_edicio','integer','reference rally.rally_edicio'),
         Field('tanda_horari_parc_tancat','integer'),
         Field('tanda_maximinscrits','integer'),
         primarykey=['tanda_nom','tanda_rally','tanda_edicio'])         

尝试引用参考集会.rally_nom' &安培;&安培; '引用rally.rally_edicio',但不行。

我认为这里的主要问题是格式的使用。我尝试使用代表,但表格不能正常工作:

 Field('tanda_rally',represent= db.rally.rally_nom),

以及lambda函数:

  format=lambda r: '%s %s' % (r.rally_nom, r.rally_edicio)) 

我的英语有点不好,但是,任何想法?票证错误始终相同:

type' exceptions.KeyError' ' rally_edicio'

函数参数列表(self =,table =,migrate = True,fake_migrate = False,polymodel = None)

TraceBack错误:

 Traceback (most recent call last):
  File "/home/usuari/Escritorio/web2py/gluon/restricted.py", line 219, 
 in restricted exec(ccode, environment)
  File "/home/usuari/Escritorio/web2py/applications/TFG_PROVES/models/myDb.py", line 76, in <module>
primarykey=['tanda_nom','tanda_rally','tanda_edicio'])
 File "/home/usuari/Escritorio/web2py/gluon/packages/dal/pydal/base.py", line 586, in define_table
table = self.lazy_define_table(tablename, *fields, **args)
 File "/home/usuari/Escritorio/web2py/gluon/packages/dal/pydal/base.py", line 620, in lazy_define_table
polymodel=polymodel)
 File "/home/usuari/Escritorio/web2py/gluon/packages/dal/pydal/adapters/base.py", line 797, in create_table
return self.migrator.create_table(*args, **kwargs)
  File "/home/usuari/Escritorio/web2py/gluon/packages/dal/pydal/migrator.py", line 221, in create_table
fk_fields = [table[rfields[k]] for k in rtable._primarykey]
KeyError: 'rally_edicio'

变量 fk_fields未定义 rtable._primarykey [&#39; rally_nom&#39;,&#39; rally_edicio&#39;] rfields {&#39; rally_nom&#39;:&#39; tanda_rally&#39;} rtable 表 k&#39; rally_edicio&#39;

来自支持web2py的代码侦听错误:

        fields = ',\n    '.join(fields)
         for rtablename in TFK:
        rtable = db[rtablename]
        rfields = TFK[rtablename]
        pkeys = [rtable[pk]._rname for pk in rtable._primarykey]
        fk_fields = [table[rfields[k]] for k in rtable._primarykey]

        fkeys = [f._rname for f in fk_fields]
        constraint_name = self.dialect.constraint_name(
            table._raw_rname, '_'.join(f._raw_rname for f in fk_fields))
        on_delete = list(set(f.ondelete for f in fk_fields))

0 个答案:

没有答案