使用alembic操作的批量插入在主键上失败

时间:2017-03-14 08:07:57

标签: python mysql sqlalchemy alembic dml

我想用我的alembic迁移文件创建一个新表,并在表上添加2条记录。我的<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listItem" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:orientation="vertical" android:padding="5dp"> <ImageView android:id="@+id/imageView1" android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginEnd="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:padding="2dp" android:src="@drawable/saffron_flag" /> <Button android:id="@+id/View_btn" android:layout_width="60dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:background="@drawable/saffron_flag" android:paddingLeft="5dp" android:textColor="@android:color/white" android:textSize="7sp" /> <TextView android:id="@+id/Name_txt" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toLeftOf="@+id/View_btn" android:layout_toRightOf="@+id/imageView1" android:text="@string/arabella" android:textColor="@color/colorPrimaryDark" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/Brandname_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Name_txt" android:layout_marginLeft="5dp" android:layout_toLeftOf="@+id/View_btn" android:layout_toRightOf="@+id/imageView1" android:text="@string/arabella" android:textColor="@color/black_overlay" android:textSize="11sp" /> <TextView android:id="@+id/Description_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Brandname_txt" android:layout_marginLeft="5dp" android:layout_toLeftOf="@+id/View_btn" android:layout_toRightOf="@+id/imageView1" android:text="@string/arabella" android:textColor="@color/black_overlay" android:textSize="11sp" /> <TextView android:id="@+id/Phone_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_marginLeft="5dp" android:layout_below="@+id/Description_txt" android:layout_toRightOf="@+id/imageView1" android:text="@string/arabella" android:textColor="@color/black_overlay" android:textSize="11sp" /> <TextView android:id="@+id/Viewed_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/View_btn" android:layout_alignBottom="@+id/View_btn" android:layout_marginEnd="25dp" android:layout_marginRight="25dp" android:layout_toLeftOf="@+id/View_btn" android:layout_toStartOf="@+id/View_btn" android:text="@string/viewed" android:textColor="@color/colorPrimaryDark" android:textSize="11sp" android:textStyle="bold" android:visibility="gone" /> <ImageView android:id="@+id/imgView" android:layout_width="30dp" android:layout_height="20dp" android:layout_alignBottom="@+id/Name_txt" android:layout_toLeftOf="@+id/Viewed_txt" android:layout_toStartOf="@+id/Viewed_txt" android:src="@drawable/ic_small_view" android:visibility="gone" /> </RelativeLayout> 功能

upgrade()

通过alembic运行升级时出现以下错误

def upgrade():
    op.create_table(
        'new_table',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('text', sa.String(length=180), nullable=False),
        sa.PrimaryKeyConstraint('id', name='pk_new_table'),
        sa.UniqueConstraint('text', name='uq_new_table__text'),
    )

    # Create ad-hoc table as a helper
    new_table = sa.table(
        'new_table',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('text', sa.String(length=180), nullable=False),
        sa.PrimaryKeyConstraint('id', name='pk_new_table'),
        sa.UniqueConstraint('text', name='uq_new_table__text'),
    )
    op.bulk_insert(new_table, [
        {'text': 'First'},
        {'text': 'Second'}
    ])

此错误从包含SqlAlchemy表上的AttributeError: 'PrimaryKeyConstraint' object has no attribute 'key' 的行开始。可能有什么不对?

应用迁移的数据库后端是MySQL服务器。

0 个答案:

没有答案