Danitsu
Danitsu

Reputation: 45

SqlAlchemy TypeError: Incompatible collection type: Line is not list-like

I have made this Schema and Insertion, I got a relationship between Line and Bar, and each Bar have a relationship also with NameBar (This is just a resume schema). My problem is that every time Im trying to do an insert I got a problem of how to make the relationship, this is my first time using and doing something with SQLAlchemy.

Schema

class Line(Base):
   __tablename__ = 'line' # Nombre de la tabla 

   id = Column(Integer, primary_key = True)
   fechaEntrada = Column(DateTime, default = datetime.utcnow)
   nameLine = Column(String(250))
   bar_id = Column(Integer, ForeignKey('bar.id'))

   bar = relationship("Bar", foreign_keys = 'Line.bar_id', backref = 'relat') 

class Bar(Base):
   __tablename__ = 'bar'
   id = Column(Integer, primary_key = True)
   zona = Column(String(250))
   nameBar_id = Column(Integer, ForeignKey('nameBar.id'))
   line_id = Column(Integer, ForeignKey('line.id'))

   nameBar = relationship("NameBar", foreign_keys = 'Bar.nameBar_id' , backref = 'rela')
   line = relationship("Line", foreign_keys = 'Bar.line_id') # Relacion con la linea

class NameBar(Base):
   __tablename__ = 'nameBar'
   id = Column(Integer, primary_key = True)
   nameBar1 = Column(String(250))
   nameBar2 = Column(String(250))
   bar_id = Column(Integer, ForeignKey('bar.id'))

   bar = relationship("Bar", foreign_keys = 'NameBar.bar_id')

Inserts

  newLine = Line(fechaEntrada = '2017-01-01 12:00:00', nameLine = 'Lala')
  newBar = Bar(zona = 'uuh', relat = newLine)
  newNameBar = NameBar(rela = newBar , nameBar1 = 'hi', nameBar2 = 'hi2')

  session.add(newLine)
  session.add(newNameBar)
  session.add(newBar)
session.commit()

So, what im trying to do here is something like; every time I have the nameLine I also want to insert the name of the Bar, so I want to insert that nameBar when I insert the newLine, so I can have the relationship and the id that is related to Bar.

Hope someone can help me and understan what Im trying to said. Thanks!

Upvotes: 1

Views: 4762

Answers (1)

fabio.sussetto
fabio.sussetto

Reputation: 7055

I think the error you're getting is because of the fact SQLAlchemy expects a one-to-many relationship between Bar and Line, so that one Bar has many Lines. Because of this, bar.relat would be a list of Lines, and not just a single instance.

So when you try to do newBar = Bar(zona = 'uuh', relat = newLine) SQLAlchemy expects a list of newLines for the "relat" kwarg and it complains about it.

Try and do this instead:

newBar = Bar(zona = 'uuh', relat = [newLine])

Alternatively, for cases where you do not want a one-to-many but a one-to-one relationship, you should use the uselist=False kwarg for the relationship() call, see: http://docs.sqlalchemy.org/en/latest/orm/relationship_api.html#sqlalchemy.orm.relationship.params.uselist

In this case, you can assign (and retrieve back) just a single instance and not a list.

Hope it's clear!

Upvotes: 4

Related Questions