Esempio n. 1
0
 class Meta:  # pylint: disable=missing-class-docstring
     database = _DATABASE
     legacy_table_names = False  # This will become a default in peewee>=4
     primary_key = peewee.CompositeKey("channel", "feed", "url")
     indexes = (
         # (('channel', 'feed', 'url'), True),  # Not needed per EXPLAIN QUERY PLAN due to sqlite_autoindex_post_1.
         (("channel", "url"), False), )  # True means unique.
Esempio n. 2
0
 class Meta:
     database = _DATABASE_V2
     legacy_table_names = False  # This will become a default in peewee>=4
     primary_key = peewee.CompositeKey('channel', 'feed', 'url')
     indexes = (
         # (('channel', 'feed', 'url'), True),  # Not needed per EXPLAIN QUERY PLAN due to sqlite_autoindex_post_1.
         (('channel', 'url'), False), )  # True means unique.
Esempio n. 3
0
 class Meta:
     indexes = [
         pv.SQL(
             "CREATE INDEX idx_templates_name_ntid on templates (name, ntid)"
         ),
         pv.SQL("CREATE INDEX idx_templates_usn on templates (usn)"),
     ]
     primary_key = pv.CompositeKey("ntid", "ord")
     without_rowid = True
Esempio n. 4
0
 class Meta:
     primary_key = peewee.CompositeKey(
         'endpoint',
         'marketplace_id',
         'refresh_token',
         'aws_access_key',
         'path',
         'data',
         'params',
         'method',
     )
     database = db_proxy
Esempio n. 5
0
    def primary_key(self, name):
        """
        Add a primary key to the model.
        This has some special cases, which is why it's not handled like all the other column types.

        :param name: Name of column.
        :return: None
        """
        meta = self.model._meta
        pk_field = peewee.CompositeKey([name])
        meta.primary_key = pk_field
        meta.add_field(name, pk_field)

        field = peewee.AutoField(column_name=name)
        meta.add_field(name, field)
Esempio n. 6
0
 class Meta:
     db_table = 'relations'
     primary_key = peewee.CompositeKey(
         'entity_one_type',
         'entity_one_id',
         'entity_two_type',
         'entity_two_id',
         'role',
     )
     indexes = (
         (('entity_one_type', 'entity_one_id', 'entity_two_type',
           'entity_two_id', 'role'), True),
         (('entity_two_type', 'entity_two_id', 'entity_one_type',
           'entity_one_id', 'role'), True),
     )
Esempio n. 7
0
 class Meta:
     primary_key = pw.CompositeKey('source', 'is_official', 'spec')  # 复合主键
Esempio n. 8
0
 class Meta:
     primary_key = pw.CompositeKey('section', 'order')
Esempio n. 9
0
 class Meta:
     primary_key = pw.CompositeKey('member', 'case')
Esempio n. 10
0
 class Meta:
     primary_key = pw.CompositeKey('price_range', 'brand')
Esempio n. 11
0
 class Meta:
     db_table = 'user_product'
     primary_key = pw.CompositeKey('user_id', 'product_id')
Esempio n. 12
0
 class Meta:
     database = db
     primary_key = pw.CompositeKey('project', 'pycampista')
Esempio n. 13
0
 class Meta:
     database = db
     primary_key = pw.CompositeKey('project', 'owner')
Esempio n. 14
0
    def reflect(self):
        """Adds fields and indexes to the model using reflection."""

        meta = self._meta
        database = meta.database
        table_name = meta.table_name
        schema = meta.schema

        if not database or not database.connected:
            return

        # Lists tables in the schema. This is a bit of a hack but
        # faster than using database.table_exists because it's cached.
        database.get_fields(table_name, schema)  # Force caching of _metadata.
        schema_tables = database._metadata[schema].keys()

        if table_name not in schema_tables:
            return

        for index in meta.indexes:
            if hasattr(index, 'reflected') and index.reflected:
                meta.indexes.remove(index)

        if not database.is_connection_usable():
            raise peewee.DatabaseError('database not connected.')

        if hasattr(meta, 'reflection_options'):
            opts = meta.reflection_options
            skip_fks = opts.get('skip_foreign_keys', False)
            use_peewee_reflection = opts.get('use_peewee_reflection', True)
        else:
            skip_fks = False
            use_peewee_reflection = True

        try:

            if use_peewee_reflection:

                # Check for locks. We only need to do this if using the Peewee
                # reflection because one of the queries it does can be blocked
                # by a AccessExclusiveLock lock.
                locks = is_table_locked(database, table_name)
                if locks and 'AccessExclusiveLock' in locks:
                    warnings.warn(
                        f'table {schema}.{table_name} is locked and '
                        'will not be reflected.', SdssdbUserWarning)
                    return

                introspector = database.get_introspector(schema)
                reflected_model = introspector.generate_models(
                    table_names=[table_name])[table_name]
                fields = reflected_model._meta.fields

            else:
                fields = database.get_fields(table_name, schema)
                fields = {field.column_name: field for field in fields}

        except KeyError as ee:
            warnings.warn(
                f'reflection failed for {table_name}: '
                f'table or column {ee} not found.', SdssdbUserWarning)
            return

        except Exception as ee:
            warnings.warn(f'reflection failed for {table_name}: {ee}',
                          SdssdbUserWarning)
            return

        for field_name, field in fields.items():

            if field_name in keyword.kwlist:
                field_name += '_'

            if field_name in meta.fields:
                meta_field = meta.fields[field_name]
                if not getattr(meta_field, 'reflected', False):
                    continue

            if isinstance(field, peewee.ForeignKeyField) and skip_fks:
                continue

            if field.primary_key:
                meta.set_primary_key(field_name, field)
            else:
                meta.add_field(field_name, field)

            meta.fields[field_name].reflected = True

        # Composite keys are not a normal column so if the pk has not been
        # set already, check if it exists in the reflected model. We avoid
        # adding pks that are foreign keys.
        if not meta.primary_key:
            if use_peewee_reflection and reflected_model._meta.primary_key:
                pk = reflected_model._meta.primary_key
                if not isinstance(pk, peewee.ForeignKeyField) or not skip_fks:
                    meta.set_primary_key(pk.name, pk)
            elif not use_peewee_reflection:
                pk = database.get_primary_keys(table_name, schema)
                if len(pk) > 1:
                    pk = peewee.CompositeKey(*pk)
                    meta.set_primary_key('__composite_key__', pk)
Esempio n. 15
0
 class Meta:
     primary_key = peewee.CompositeKey("multicloud_stack", "cloud_name")
Esempio n. 16
0
 class Meta:
     database = db
     primary_key = pw.CompositeKey('url', 'group')
Esempio n. 17
0
 class Meta:
     db_table = "karma_user"
     primary_key = pw.CompositeKey("userid", "chatid")
Esempio n. 18
0
 class Meta:
     database = data_db
     primary_key = peewee.CompositeKey('address', 'perm_type')
Esempio n. 19
0
 class Meta:
     primary_key = pw.CompositeKey('party', 'case')
Esempio n. 20
0
 class Meta:
     db_table = 'user_task'
     primary_key = pw.CompositeKey('user_id', 'task_id')
Esempio n. 21
0
 class Meta:
     primary_key = pw.CompositeKey('brand', 'model')
Esempio n. 22
0
 class Meta:
     db_table = 'weather'
     primary_key = peewee.CompositeKey('city_id', 'wdate')
Esempio n. 23
0
 class Meta:
     primary_key = pw.CompositeKey('entity', 'element', 'start', 'end')
Esempio n. 24
0
 class Meta:
     primary_key = pw.CompositeKey('image', 'tag')
Esempio n. 25
0
 class Meta:
     database = proxy
     primary_key = peewee.CompositeKey('serie_id', 'indice_tiempo')
Esempio n. 26
0
 class Meta:
     """This class defines a composite key as the primary key."""
     logger.info(
         "Set primary key to combo of person_employed & start_date fields.\n"
     )
     primary_key = pw.CompositeKey('person_employed', 'start_date')
Esempio n. 27
0
 class Meta(S.BaseMeta):
     database = db
     primary_key = pw.CompositeKey('subject_id', 'text')
Esempio n. 28
0
 class Meta:
     '''
         Metaclass to assign the primary key
     '''
     primary_key = pw.CompositeKey('party', 'case')
Esempio n. 29
0
 class Meta:
     primary_key = pw.CompositeKey('shop_code', 'sku')
 class Meta:
     database = db
     primary_key = peewee.CompositeKey('chat_id', 'link')