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'))



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
-- 
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1

mQENBFQIluABCACfPwAhRAwFD3NXgv5CtVUGSiqdfJGVViVBqaKd+14E0pASA0MU
G0Ewj7O7cGy/ZIoiZ0+lIEZmzJKHfuGwYhXjR/PhnUDrQIHLBvh9WuD6JQuULXfH
kXtVm/i9wm76QAcvr2pwYgNzhcJntUHl2GcgnInYbZDeVmg+p9yIPJjuq73/lRS3
0/McgNoFOBhKK/S6STQuFyjr9OyJyYd1shoM3hmy+kg0HYm6OgQBJNg92WV9jwGe
GzlipvEp2jpLwVsTxYir2oOPhfd9D1fC9F/l/3gXbfjd5GIIVrZFq2haZmoVeJ33
LJxo3RA5Tf9LoUeels1b4s9kFz6h7+AHERUpABEBAAG0IUNocmlzdGlhbiBCdWh0
eiA8YnVodHpAcG9zdGVvLmRlPokBPgQTAQIAKAUCVAiW4AIbAwUJAeEzgAYLCQgH
AwIGFQgCCQoLBBYCAwECHgECF4AACgkQZLsXsAdRqOxNUAf/V/hDA5zGDpySuCEj
DhjiVRK74J9Wd8gfH0WAf1Co5HZ24wZH8rgOIVIgXw8rWkOw/VA6xfdfT+64xjTY
Fhkpbrk199nDzp72F7Jc4NC+x8xac2e3rK5ifSWhZx7L5A32pGYE+d16m3EEqImK
D4gcZl38x9zdUnD4hHyXkIPz1uCfuMuGgWEnaUk4Wbj41CBZr3O0ABue6regV15U
jaes8r+B8iCcY+0yP2kse+3iaCaMqNv5FgQZ9+b2Cql8pFkZJVtBVUw4GW3DWZJi
du0O/YrC9TgS+xY9ht/MD2qSHwjcK1sdImjqBO7xP8TIOwKeYyDvGKnSO3EJ/sSA
UPGEPrkBDQRUCJbgAQgA0k/Qg67CCUJE2/zuxBEoK4wLJpDRJzh8CQPZpjWx8VP0
KL892jwfxymXn8KNhuy1SgCBFSeV9jg4VZNWDlUGJc2lo82ajr9PzIsrQwu4lf0B
zrUWV5hWepKu/kb8uSjx58YYfx0SFz4+9akX3Wwu9TUHntzL5Gk3Q26nnsr1xEJ+
VEumvCH9AE0Tk0K7dQpJ2/JcLuO+uhrpd/lHFDYVN5NsG3P015uFOkDI6N/xNFCj
v95XNR93QlfKpK3qWlFGescfG+o/7Ub6s67/i/JoNbw0XgPEHmQfXpD7IHO4cu+p
+ETb11cz+1mmi96cy98ID+uTiToJ8G//yD9rmtyxoQARAQABiQElBBgBAgAPBQJU
CJbgAhsMBQkB4TOAAAoJEGS7F7AHUajs6sQH/iKs6sPc0vkRJLfbwrijZeecwCWF
blo/jzIQ8jPykAj9SLjV20Xwqg3XcJyko8ZU6/zuRJq9xjlv9pZr/oVudQAt6v+h
2Cf4rKEjmau483wjMV2xjTXQhZi9+ttDbia4fgdmGtKsOicn5ae2fFXcXNPu3RiW
sZKifWdokA6xqMW6iIG9YjjI5ShxngHWp2xfPscBFMDRtFOMags/Yx+YvwoyEZ4A
dURYMFHFqpwILEc8hIzhRg1gq40AHbOaEdczS1Rr3T7/gS6eBs4u6HuY5g2Bierm
lLjpspFPjMXwJAa/XLOBjMF2vsHPrZNcouNKkumQ36yq/Pm6DFXAseQDxOk=
=PGP9
-----END PGP PUBLIC KEY BLOCK-----

-- 
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