@@ -47,7 +47,7 @@ def define_violation(dbengine):
         __tablename__ = violations_tablename
 
         id = Column(Integer, primary_key=True)
-        model_handle = Column(String(256))
+        inventory_id = Column(String(256))
         resource_id = Column(String(256), nullable=False)
         resource_type = Column(String(256), nullable=False)
         rule_name = Column(String(256))
# Copyright 2017 The Forseti Security Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Database access objects for Forseti Scanner. """

import json

from sqlalchemy import Column
from sqlalchemy import String, Integer, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from google.cloud.forseti.services import db


# pylint: disable=no-member

def define_violation(dbengine):
    """Defines table class for violations.

    A violation table will be created on a per-model basis.

    Args:
        dbengine (engine): sqlalchemy database engine

    Returns:
        ViolationAcccess: facade for accessing violations.
    """

    base = declarative_base()
    violations_tablename = 'violations'

    class Violation(base):
        """Row entry for a violation."""

        __tablename__ = violations_tablename

        id = Column(Integer, primary_key=True)
        model_handle = Column(String(256))
        resource_id = Column(String(256), nullable=False)
        resource_type = Column(String(256), nullable=False)
        rule_name = Column(String(256))
        rule_index = Column(Integer, default=0)
        violation_type = Column(String(256), nullable=False)
        data = Column(Text)

        def __repr__(self):
            """String representation.

            Returns:
                str: string representation of the Violation row entry.
            """
            string = ("<Violation(violation_type='{}', resource_type='{}' "
                      "rule_name='{}')>")
            return string.format(
                self.violation_type, self.resource_type, self.rule_name)

    class ViolationAccess(object):
        """Facade for violations, implement APIs against violations table."""
        TBL_VIOLATIONS = Violation

        def __init__(self, dbengine):
            """Constructor for the Violation Access.

            Args:
                dbengine (engine): sqlalchemy database engine
            """
            self.engine = dbengine
            self.violationmaker = self._create_violation_session()

        def _create_violation_session(self):
            """Create a session to read from the models table.

            Returns:
                ScopedSessionmaker: A scoped session maker that will create
                    a session that is automatically released.
            """
            return db.ScopedSessionMaker(
                sessionmaker(
                    bind=self.engine,
                    expire_on_commit=False),
                auto_commit=True)

        def create(self, violations, model_handle):
            """Save violations to the db table.

            Args:
                violations (list): A list of violations.
                model_handle (str): Name of the model that the violations
                    originate from.
            """
            with self.violationmaker() as session:
                for violation in violations:
                    violation = self.TBL_VIOLATIONS(
                        model_handle=model_handle,
                        resource_id=violation.get('resource_id'),
                        resource_type=violation.get('resource_type'),
                        rule_name=violation.get('rule_name'),
                        rule_index=violation.get('rule_index'),
                        violation_type=violation.get('violation_type'),
                        data=json.dumps(violation.get('violation_data'))
                    )
                    session.add(violation)

        def list(self):
            """List all violations from the db table.

            Returns:
                list: List of Violation row entry objects.
            """
            with self.violationmaker() as session:
                return session.query(self.TBL_VIOLATIONS).all()


    base.metadata.create_all(dbengine)

    return ViolationAccess

Should this be inventory_id or inventory_index_id? I think it's better to have this as a foreign key and reference back to the inventory/inventory_index table.