【发布时间】:2018-07-30 04:10:25
【问题描述】:
问题:简单地说,我正在尝试在 SQLAlchemy ORM 表的主键已经定义之后重新定义它。
示例:
class Base:
@declared_attr
def __tablename__(cls):
return f"{cls.__name__}"
@declared_attr
def id(cls):
return Column(Integer, cls.seq, unique=True,
autoincrement=True, primary_key=True)
Base = declarative_base(cls=Base)
class A_Table(Base):
newPrimaryKeyColumnsDerivedFromAnotherFunction = []
# Please Note: as the variable name tries to say,
# these columns are auto-generated and not known until after all
# ORM classes (models) are defined
# OTHER CLASSES
def changePriKeyFunc(model):
pass # DO STUFF
# Then do
Base.metadata.create_all(bind=arbitraryEngine)
# After everything has been altered and tied into a little bow
*请注意,这是对我要解决的真正问题的简化。
可能的解决方案:您的第一个想法可能是这样做:
def possibleSolution(model):
for pricol in model.__table__.primary_key:
pricol.primary_key = False
model.__table__.primary_key = PrimaryKeyConstraint(
*model.newPrimaryKeyColumnsDerivedFromAnotherFunction,
# TODO: ADD all the columns that are in the model that are also a primary key
# *[col for col in model.__table__.c if col.primary_key]
)
但是,这不起作用,因为在尝试添加、刷新和提交时,会抛出错误:
InvalidRequestError: Instance <B_Table at 0x104aa1d68> cannot be refreshed -
it's not persistent and does not contain a full primary key.
即使这样:
In [2]: B_Table.__table__.primary_key
Out[2]: PrimaryKeyConstraint(Column('a_TableId', Integer(),
ForeignKey('A_Table.id'), table=<B_Table>,
primary_key=True, nullable=False))
还有这个:
In [3]: B_Table.__table__
Out[3]: Table('B_Table', MetaData(bind=None),
Column('id', Integer(), table=<B_Table>, nullable=False,
default=Sequence('test_1', start=1, increment=1,
metadata=MetaData(bind=None))),
Column('a_TableId', Integer(),
ForeignKey('A_Table.id'), table=<B_Table>,
primary_key=True, nullable=False),
schema=None)
最后:
In [5]: b.a_TableId
Out[5]: 1
另请注意,数据库实际上反映了更改的(和真实的)主键,所以我知道 ORM/SQLAlchemy 发生了一些事情。
问题:综上所述,在模型已经定义好之后,如何更改模型的主键?
编辑:完整代码见下文(相同类型的错误,仅在 SQLite 中)
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declared_attr, declarative_base
from sqlalchemy.schema import PrimaryKeyConstraint
from sqlalchemy import Sequence, create_engine
class Base:
@declared_attr
def __tablename__(cls):
return f"{cls.__name__}"
@declared_attr
def seq(cls):
return Sequence("test_1", start=1, increment=1)
@declared_attr
def id(cls):
return Column(Integer, cls.seq, unique=True, autoincrement=True, primary_key=True)
Base = declarative_base(cls=Base)
def relate(model, x):
"""Model is the original class, x is what class needs to be as
an attribute for model"""
attributeName = x.__tablename__
idAttributeName = "{}Id".format(attributeName)
setattr(model, idAttributeName,
Column(ForeignKey(x.id)))
setattr(model, attributeName,
relationship(x,
foreign_keys=getattr(model, idAttributeName),
primaryjoin=getattr(
model, idAttributeName) == x.id,
remote_side=x.id
)
)
return model.__table__.c[idAttributeName]
def possibleSolution(model):
if len(model.defined):
newPriCols = []
for x in model.defined:
newPriCols.append(relate(model, x))
for priCol in model.__table__.primary_key:
priCol.primary_key = False
priCol.nullable = True
model.__table__.primary_key = PrimaryKeyConstraint(
*newPriCols
# TODO: ADD all the columns that are in the model that are also a primary key
# *[col for col in model.__table__.c if col.primary_key]
)
class A_Table(Base):
pass
class B_Table(Base):
defined = [A_Table]
possibleSolution(B_Table)
engine = create_engine('sqlite://')
Base.metadata.create_all(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()
a = A_Table()
b = B_Table(A_TableId=a.id)
print(B_Table.__table__.primary_key)
session.add(a)
session.commit()
session.add(b)
session.commit()
【问题讨论】:
-
我的第一个想法是保留自动增量主键,并为您的“其他”键值设置另一列索引。这可能吗?
-
@SuperShoot 您可以假设 id 列没有被删除。至于有另一个专栏,我不确定你的意思。
-
我的意思是在你的表中添加另一列,命名为
other_id,在其上放置一个索引以便快速查询,并将该列的值设置为可以仅在已创建对象后才生成,以免您在 PK 存在时弄乱它。如果您无法更改架构,那么这不是一个选项,但这就是我要问的原因。 -
对不起,我刚刚意识到您不想更改存储在表中的行的主键值,您想在实际创建表之前更改表的主键定义数据库基于与 ORM 类本身的创建相关的一些变量,对吗?我的错。
-
请提供一个明确的问题。为什么要重新定义模型,而不是简单地以正确的形式创建它们?这个问题对您来说似乎毫无意义,但请注意您的用例似乎相当罕见。同时提供minimal reproducible example。给出的示例代码无法重现您遇到的错误。
标签: python orm sqlalchemy