P
P
phisher132022-04-13 19:59:31
Flask
phisher13, 2022-04-13 19:59:31

When creating an entry in the database, an error pops up: AttributeError: 'int' object has no attribute '_sa_instance_state'?

There are one-to-many relationships. When I create a new movie record, it says that there is no director with this id (the database with directors is empty), when I try to create a new director record: AttributeError: 'int' object has no attribute '_sa_instance_state'

routes.py

from flask_restx import Resource, marshal_with, Namespace, fields
from flask import request

from api.models.movie import Film, Director

movie = Namespace('movie')

schema = movie.model(
    'Film', {
        'id': fields.Integer(),
        'title': fields.String(),
        'genre': fields.String(enums=['ACTION', 'COMEDY', 'DRAMA', 'HORROR', 'FANTASY'])
    }
)

schema_dir = movie.model(
    'Director', {
        'id': fields.Integer(),
        'name': fields.String(),
        'surname': fields.String(),
        'film': fields.Integer(),
    }
)


class MovieApi(Resource):
    @marshal_with(schema)
    def get(self):
        """"
        Get all movies
        """
        films = Film.query.all()

        return films

    @marshal_with(schema)
    def post(self):
        """"
        Create new film
        """
        data = request.json
        new_film = Film(
            title=data.get('title'),
            genre=data.get('genre')
        )

        new_film.save()

        return new_film


class DirectorApi(Resource):
    @marshal_with(schema_dir)
    def get(self):
        """"
        Get all movies
        """
        directors = Director.query.all()

        return directors

    @marshal_with(schema_dir)
    def post(self):
        """"
        Create new film
        """
        data = request.json
        new_dir = Director(
            name=data.get('name'),
            surname=data.get('surname'),
            film=data.get('film')
        )

        new_dir.save()

        return new_dir


movie.add_resource(MovieApi, '/')
movie.add_resource(DirectorApi, '/director')


movie.py database model
from enum import Enum

from api.db import db


class Genres(Enum):
    ACTION = 'action'
    COMEDY = 'comedy'
    DRAMA = 'drama'
    HORROR = 'horror'
    FANTASY = 'fantasy'


class Film(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), nullable=False)
    genre = db.Column(db.Enum(Genres), default=None)
    director = db.Column(db.Integer, db.ForeignKey('director.id'), default=None)

    def __repr__(self):
        return self.title

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    @classmethod
    def get_by_name(cls, name):
        return cls.query.get_or_404(name)


class Director(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    surname = db.Column(db.String(255), nullable=False)
    film = db.relationship('Film')

    def __repr__(self):
        return self.name + ' ' + self.surname

    def save(self):
        db.session.add(self)
        db.session.commit()

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    @classmethod
    def get_by_name(cls, name):
        return cls.query.get_or_404(name)

Queries
{"name": "boris", "surname": "johnson", "film": [13]} - AttributeError: 'int' object has no attribute '_sa_instance_state'
{"name": "boris", "surname ": "johnson", "film": 13} - TypeError: Incompatible collection type: int is not list-like

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question