我使用 SQLAlchemy Joined Table Inheritance声明了两个类。另外我正在使用Flask-SQLAlchemy和 Flask。
class Parent(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
type = db.Column(db.String(50))
__mapper_args__ = {
'polymorphic_identity': 'parent',
'polymorphic_on': type
}
class Child(Parent):
id = db.Column(db.Integer, db.ForeignKey('parent.id'), primary_key=True)
brand_id = db.Column(db.Integer, nullable=False)
__mapper_args__ = {
'polymorphic_identity': 'child',
}
Run Code Online (Sandbox Code Playgroud)
父子表之间是一对一的关系,在子端可选出现。
当我创建 Child 的新实例,将其添加到会话并将其提交到数据库时,一切都按预期工作。SQLAlchemy 向INSERT父表发送一条语句,然后INSERT向子表发送另一条语句,全部由其自身完成,避免任何外键约束违规。
在应用程序的某个点,我想在子表中添加一个新行,引用父表中已经存在的行。
当我创建一个新Child实例并手动为其分配一个现有的 id 到 FK 列并将其发送到数据库时。我收到一个 IntegrityError(重复唯一约束冲突),因为 SQLAlchemy 尝试在数据库中创建一个新的父条目,并将 id 传递给子实例。
所以,真正的问题是:
我如何告诉 Sqlalchemy,父条目已经存在于数据库中,而我只想创建子条目?
AngularJS 1.x的ng-repeat是否与ES6 Maps兼容?
我正在寻找一种使用 QML 为按钮的所有状态创建自定义样式的方法。到目前为止,我只找到了以下方法来创建可以应用于多个按钮的样式
Component {
id: leftButtonStyle
ButtonStyle {
background: Rectangle {
width: control.width
height: control.height
color: "black"
}
label: Text {
font.family: "Helvetica"
font.pointSize: 10
color: "#949599"
text: control.text
}
}
}
Button {
text:"Button One"
style: leftButtonStyle
}
Button {
text:"Button Two"
style: leftButtonStyle
}
Run Code Online (Sandbox Code Playgroud)
但这只是改变按钮的正常状态,无法找到按下按钮时赋予样式的方法。
我正在尝试测试下一个角度指令.它监听ui-route $ stateChange事件以显示进度指示器.
angular.module('sm.components.progress', ['ui.router'])
.directive('smProgress', function($rootScope) {
return {
restrict: 'E',
replace: true,
template: '<div class="in-progress" ng-if="isRouteLoading">' +
'<span>.</span><span>.</span><span>.</span>' +
'<div/>',
controller: function($scope) {
$scope.isRouteLoading = false;
$rootScope.$on('$stateChangeStart',
function() {
$scope.isRouteLoading = true;
});
$rootScope.$on('$stateChangeSuccess',
function() {
$scope.isRouteLoading = false;
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
这是我的测试代码:
describe('smProgress', function() {
var $compile, $rootScope, element, scope;
beforeEach(module('sm.components.progress'));
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
scope = $rootScope.$new();
}));
afterEach(function() {
element.remove();
});
it('$rootScope.isRouteLoading should be false on start', …Run Code Online (Sandbox Code Playgroud) angularjs ×2
c++ ×1
ecmascript-6 ×1
flask ×1
jasmine ×1
python ×1
qml ×1
qt ×1
sqlalchemy ×1
unit-testing ×1