1

I am getting a neomodel.exceptions.ModelDefinitionMismatch while trying to build a simple Flask app connected to the bold port of Neo4J.

import json
from flask import Flask, jsonify, request
from neomodel import StringProperty, StructuredNode, config


class Item(StructuredNode):
    __primarykey__ = "name"
    name = StringProperty(unique_index=True)


def create_app():
    app = Flask(__name__)

    config.DATABASE_URL = 'bolt://neo4j:test@db:7687'

    @app.route('/', methods=['GET'])
    def get_all_items():
        return jsonify({'items': [item.name for item in Item.nodes]})

    @app.route('/', methods=['POST'])
    def create_item():
        item = Item()
        item.name = json.loads(request.data)['name']
        item.save()
        return jsonify({'item': item.__dict__})

    return app

Doing postrequest works; I can check in the database that the items actually are added! But the get request returns:

neomodel.exceptions.ModelDefinitionMismatch: Node with labels Item does not resolve to any of the known objects

I'm using Python 3.7.0, Flask 1.0.2 and neomodel 3.0.3

update

To give the full problem: I run the application in a Docker container with docker-compose in DEBUG mode.

The Dockerfile:

FROM continuumio/miniconda3
COPY . /app
RUN pip install Flask gunicorn neomodel==3.3.0
EXPOSE 8000
CMD gunicorn --bind=0.0.0.0:8000 - "app.app:create_app()"

The docker-compose file:

# for local development
version: '3'
services:
  db:
    image: neo4j:latest
    environment:
      NEO4J_AUTH: neo4j/test
    networks:
      - neo4j_db
    ports:
      - '7474:7474'
      - '7687:7687'

  flask_svc:
    build: .
    depends_on:
      - 'db'
    entrypoint:
      - flask
      - run
      - --host=0.0.0.0
    environment:
      FLASK_DEBUG: 1
      FLASK_APP: app.app.py
    ports:
      - '5000:5000'
    volumes:
      - '.:/app'
    networks:
      - neo4j_db

networks:
  neo4j_db:
    driver: bridge

And I run it with:

docker-compose up --build -d
Joost Döbken
  • 3,450
  • 2
  • 35
  • 79

1 Answers1

4

Try using neomodel==3.2.9

I had a similar issue and rolled back version to get it to work.

Here's the commit that broke things

Looks like they introduced _NODE_CLASS_REGISTRY under neomodel.Database, object which is meant to be a singleton. But with Flask it's not necessarily a singleton because it keeps instantiating new instances of Database with empty _NODE_CLASS_REGISTRY.

I am not sure how to get this to work with 3.3.0

Fardin
  • 73
  • 5