On 8/11/15 7:12 PM, c.bu...@posteo.jp wrote:
I want to add a new column which is a many-to-many relation to a new
created table. I am not sure what is wrong with the code below.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlalchemy as sa
import sqlalchemy.orm as sao
import sqlalchemy.ext.declarative as sad
import sqlalchemy_utils as su

import alembic
import alembic.operations
import alembic.migration

_Base = sad.declarative_base()

# a simple model
class Model(_Base):
     __tablename__ = 'Model'
     _oid = sa.Column('oid', sa.Integer, primary_key=True)
     _simple = sa.Column('simple', sa.Integer)

# engine
engine = sa.create_engine('sqlite:///mig2.db', echo = True)

# create database if needed
if not su.database_exists(engine.url):
     su.create_database(engine.url)     # create the database
     _Base.metadata.create_all(engine)  # create the table in the
db

# another model
class Sub(_Base):
     __tablename__ = 'Sub'
     _oid = sa.Column('oid', sa.Integer, primary_key=True)
     _somedata = sa.Column('simple', sa.Integer)

# many-to-many-relation
model_sub_relation = sa.Table('model_sub_relation', _Base.metadata,
     sa.Column('model_oid', sa.Integer, sa.ForeignKey('Model.oid')),
     sa.Column('sub_oid', sa.Integer, sa.ForeignKey('Sub.oid'))
)

conn = engine.connect()
ctx = alembic.migration.MigrationContext.configure(conn)
op = alembic.operations.Operations(ctx)

op.create_table('Sub',
                sa.Column('oid', sa.Integer, primary_key=True),
                sa.Column('simple', sa.Integer)
   )

op.add_column('Model', sao.relationship('subs',
secondary=model_sub_relation, backref='Model'))

a relationship() is not a Column. Alembic "add_column()" is intended to render an "ALTER TABLE <foo> ADD COLUMN <bar>" statement. You need to pass it a Column object.









Here you see the error message.

Traceback (most recent call last):
   File "./mig2.py", line 54, in <module>
     op.add_column('Model', sao.relationship('subs',
secondary=model_sub_relation, backref='Model')) File
"/usr/local/lib/python3.4/dist-packages/alembic/operations.py", line
592, in add_column t = self._table(table_name, column, schema=schema)
File "/usr/local/lib/python3.4/dist-packages/alembic/operations.py",
line 149, in _table t = sa_schema.Table(name, m, *columns, **kw) File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/sql/schema.py", line
416, in __new__ metadata._remove_table(name, schema) File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/util/langhelpers.py",
line 60, in __exit__ compat.reraise(exc_type, exc_value, exc_tb) File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/util/compat.py",
line 182, in reraise raise value File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/sql/schema.py", line
411, in __new__ table._init(name, metadata, *args, **kw) File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/sql/schema.py", line
488, in _init self._init_items(*args) File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/sql/schema.py", line
72, in _init_items item._set_parent_with_dispatch(self) File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/util/langhelpers.py",
line 833, in __getattr__ return self._fallback_getattr(key) File
"/usr/local/lib/python3.4/dist-packages/sqlalchemy/util/langhelpers.py",
line 811, in _fallback_getattr raise AttributeError(key)
AttributeError: _set_parent_with_dispatch

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to