相关疑难解决方法(0)

在Alembic迁移期间更新列内容

假设我的db模型包含一个对象User:

Base = declarative_base() 

class User(Base):                                                               
    __tablename__ = 'users'                                                     

    id = Column(String(32), primary_key=True, default=...) 
    name = Column(Unicode(100))                                             
Run Code Online (Sandbox Code Playgroud)

我的数据库包含一个包含n行的users表.在某些时候,我决定拆分成和,而在我想我的数据迁移以及.namefirstnamelastnamealembic upgrade head

自动生成的Alembic迁移如下:

def upgrade():
    op.add_column('users', sa.Column('lastname', sa.Unicode(length=50), nullable=True))
    op.add_column('users', sa.Column('firstname', sa.Unicode(length=50), nullable=True))

    # Assuming that the two new columns have been committed and exist at
    # this point, I would like to iterate over all rows of the name column,
    # split the string, write it into the new …
Run Code Online (Sandbox Code Playgroud)

sqlalchemy-migrate python-3.x alembic

13
推荐指数
2
解决办法
5330
查看次数

sqlalchemy:alembic批量插入失败:'str'对象没有属性'_autoincrement_column'

我的模型看起来像

class Category(UserMixin, db.Model):
    __tablename__ = 'categories'
    uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
                  unique=True)
    name = Column('name', String, nullable=False)
    parent = Column('parent', String, nullable=False)
    created_on = Column('created_on', sa.types.DateTime(timezone=True),
                        default=datetime.utcnow())
    __table_args__ = (UniqueConstraint('name', 'parent'),)

    def __init__(self, name, parent):
        self.name = name
        self.parent = parent

    def __repr__(self):
        return '<Category:%s:%s:%s>' % (
            self.uuid, self.name, self.category_type)
Run Code Online (Sandbox Code Playgroud)

其中GUID是自定义sqlalchemy类型我使用alembic --autogenerate选项创建表

 op.create_table('categories',
                    sa.Column('uuid', UUID(), nullable=False),
                    sa.Column('name', sa.String(), nullable=False),
                    sa.Column('parent', sa.String(), nullable=False),
                    sa.Column('created_on', sa.DateTime(timezone=True),
                              nullable=True),
                    sa.PrimaryKeyConstraint('uuid'),
                    sa.UniqueConstraint('name', 'parent'),
                    sa.UniqueConstraint('uuid')
    )
Run Code Online (Sandbox Code Playgroud)

和PostgreSQL表一样

            Table "public.categories"
   Column …
Run Code Online (Sandbox Code Playgroud)

python postgresql sqlalchemy alembic

9
推荐指数
1
解决办法
4109
查看次数

alembic 并获取最后插入的值

我正在使用 alembic 来管理我的数据库结构。

添加使用 id 作为整数和主键的表后,id 列将成为自动增量列。我如何查询升级脚本中的数据,以便我确定我得到了正确的 id(我知道在这种特定情况下它是 1)?

我知道怎么做

#creating the table
op.create_table(
    'srv_feed_return_type',
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('name', sa.String(50), nullable=False),
    sa.Column('created', sa.DateTime, server_default=func.now(), nullable=False),
    sa.Column('created_by', sa.String(50), nullable=False),
    sa.Column('last_updated', sa.DateTime, nullable=False),
    sa.Column('last_updated_by', sa.String(50), nullable=False)
)

#table for operations
srv_feed_return_type = table('srv_feed_return_type',
                             column('name'),
                             column('created'),
                             column('created_by'),
                             column('last_updated'),
                             column('last_updated_by'))

#bulk insert
op.bulk_insert(srv_feed_return_type,
               [
                   {'name': 'dataset',
                    'created': datetime.now(), 'created_by': 'Asken',
                    'last_updated': datetime.now(), 'last_updated_by': 'Asken'}
               ])
Run Code Online (Sandbox Code Playgroud)

我知道我可以进行更新,但是如何使用类似下面的内容进行选择?

op.execute(
    srv_feed_return_type.update().\
        where(srv_feed_return_type.c.name==op.inline_literal('dataset')).\
        values({'name':op.inline_literal('somethingelse')})
        )
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy alembic

4
推荐指数
1
解决办法
2668
查看次数