Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Sequence #96

Closed
matemax opened this issue Jan 6, 2019 · 1 comment · Fixed by #97
Closed

Support Sequence #96

matemax opened this issue Jan 6, 2019 · 1 comment · Fixed by #97

Comments

@matemax
Copy link
Contributor

matemax commented Jan 6, 2019

HI, i am using Sequence in my models.

SEQUENCE = Sequence('my_sequence', start=1)

class MyTabel(Base):

    __tablename__ = 'tabel_1'
    Base.metadata = metadata

    id_2 = Column(String(36), ForeignKey('tabel_2.id_2 ', ondelete='CASCADE'), primary_key=True)

    id_1 = Column(String(36), ForeignKey('tabel_2.id_1 ', ondelete='CASCADE'), primary_key=True)

    seq_id= Column(Integer,  SEQUENCE )

And when i try insert into the table with sequence a new value i have following error:

  File "C:\Program Files\Python37\lib\site-packages\asyncpg\connection.py", line 433, in fetchval
    data = await self._execute(query, args, 1, timeout)
  File "C:\Program Files\Python37\lib\site-packages\asyncpgsa\connection.py", line 89, in _execute
    query, compiled_args = compile_query(query, dialect=self._dialect)
  File "C:\Program Files\Python37\lib\site-packages\asyncpgsa\connection.py", line 64, in compile_query
    query = execute_defaults(query)  # default values for Insert/Update
  File "C:\Program Files\Python37\lib\site-packages\asyncpgsa\connection.py", line 40, in execute_defaults
    _execute_default_attr(query, query.parameters, attr_name)
  File "C:\Program Files\Python37\lib\site-packages\asyncpgsa\connection.py", line 48, in _execute_default_attr
    if attr.is_scalar:
AttributeError: 'Sequence' object has no attribute 'is_scalar'

Following change fix it in my case. But i am not sure that is correct.

# old
def _execute_default_attr(query, param, attr_name):
    for col in query.table.columns:
        attr = getattr(col, attr_name)
        if attr and param.get(col.name) is None:
            if attr.is_scalar:
                param[col.name] = attr.arg
            elif attr.is_callable:
                param[col.name] = attr.arg({})

# new
def _execute_default_attr(query, param, attr_name):
    for col in query.table.columns:
        attr = getattr(col, attr_name)
        if attr and param.get(col.name) is None:
            if attr.is_sequence:
                continue
            if attr.is_scalar:
                param[col.name] = attr.arg
            elif attr.is_callable:
                param[col.name] = attr.arg({})

asyncpgsa - Version: 0.25.2
sqlalchemy - Version: 1.2.15

@nhumrich
Copy link
Contributor

Sweet. Sorry I am getting so late to this. If you would like to open up a pull request, requesting this change, with a couple tests, I would be happy to review it and likely merge it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants