aiopg icon indicating copy to clipboard operation
aiopg copied to clipboard

error when inserting multiple rows

Open samuelcolvin opened this issue 8 years ago • 4 comments

This was the error I was getting when I posted #250, however that issue is about something else so I thought preferable to create a new issue.

I'm getting the exception below sometimes when running insert with multiple values.

What's odd is I don't seem to always get it but I can't work out what's correlating with the exception happening. I never get it with single value inserts.

    cur = await conn.execute(sa_subjects.insert().values(values))
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/utils.py", line 72, in __await__
    resp = yield from self._coro
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/sa/connection.py", line 88, in _execute
    compiled_parameters = [compiled.construct_params(dp)]
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/sa/engine.py", line 25, in construct_params
    pd[column.key] = self._exec_default(column.default)
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/sa/engine.py", line 35, in _exec_default
    return default.arg
AttributeError: 'Sequence' object has no attribute 'arg'

If I print default I get:

Sequence('subject_id_seq', metadata=MetaData(bind=None))

This appears to be the sequence object I created on the primary key field of the table.

Sequence doesn't have an arg field, it looks like _exec_default is expecting a ColumnDefault not Sequence, I can't work out why it's receiving a Sequence not ColumnDefault sometimes.

The table is very simple, just

class Subject(Base):
    __tablename__ = 'subjects'

    id = Column(Integer, Sequence('subject_id_seq'), primary_key=True, nullable=False)
    name = Column(String(63), nullable=False, index=True)
    category = Column(String(63), nullable=False, index=True)

    __table_args__ = (
        UniqueConstraint('name', 'category', name='_subject_name_cat'),
    )

sa_subjects = Subject.__table__

samuelcolvin avatar Jan 13 '17 22:01 samuelcolvin

I think the intermittency was because either: the table didn't have a sequence on the primary key (different table from above) or there was only one value in values the standard "create a single item" logic was employed.

Otherwise I seem to get this error all the time.

samuelcolvin avatar Jan 13 '17 23:01 samuelcolvin

@vir-mir could you please take a look on this issue?

jettify avatar Jan 13 '17 23:01 jettify

Ok, this issue is related to the explicit use of Sequence('subject_id_seq') removing that (and thereby using the default SERIAL as per this) seems to solve the problem.

Still an error through, currently as far as I can tell Sequence breaks insert() with multiple values.

samuelcolvin avatar Jan 13 '17 23:01 samuelcolvin

@samuelcolvin @jettify Hi. I'm look to next week.

vir-mir avatar Jan 14 '17 21:01 vir-mir