语句形式为:Insert into Table2(field1,field2,…) select value1,value2,… from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中
语句形式为:Insert into Table2(field1,field2,…) select value1,value2,… from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中
1 | @event.listens_for(db.Model, 'before_update', propagate=True) |
创建sequence
CREATE SEQUENCE production_id_seq START 547 OWNED BY production.f_id;
修改sequence
ALTER SEQUENCE production_id_seq OWNED BY production.f_id;
设置字段所使用的sequence
1 | dbname=# alter table production alter column f_id set default nextval('production_id_seq'::regclass); |
重置序列
1 | dbname=# \d production_id_seq |
参考资料:
在Postgres里用Sequence
default vs server_default
orm默认值 vs 数据库默认值
onupdate vs server_onupdate
orm自动更新 vs 数据库自动更新
db.engine.execute vs db.session.execute
engine vs session vs connection
db.DateTime vs db.TIMESTAMP
表字段默认值default必须传方法名,不带()
1 | addtime = db.Column('f_addtime', db.TIMESTAMP, default=datetime.now) |
参考资料:
SQLAlchemy docs
SQLAlchemy: engine, connection and session difference
flask-sqlalchemy中 backref lazy的参数实例解释和选择
SQLalchemy relationship之lazy属性 学习笔记
ORM SQLAlchemy - 建立一个关系 relationship
1 | SELECT 1::INTEGER; |
Operator | Right Operand Type | Description | Example | ||
---|---|---|---|---|---|
@> |
jsonb |
Does the left JSON value contain within it the right value? | '{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb |
||
<@ |
jsonb |
Is the left JSON value contained within the right value? | '{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb |
||
? |
text |
Does the key/element string exist within the JSON value? | '{"a":1, "b":2}'::jsonb ? 'b' |
||
`? | ` | text[] |
Do any of these key/element strings exist? | `’{“a”:1, “b”:2, “c”:3}’::jsonb ? | array[‘b’, ‘c’]` |
?& |
text[] |
Do all of these key/element strings exist? | '["a", "b"]'::jsonb ?& array['a', 'b'] |
1 | SELECT '{"模型分类": ["人物", "道具"], "模型面数": ["低模"]}'::jsonb->'模型分类' @> '["人物","道具"]'; |
参考资料:
9.15. JSON Functions and Operators
JSON Functions and Operators
8.14. JSON Types
Querying JSON in Postgres
PostgreSQL 9.4.4 中文手册
PostgreSQL 9.4.4 中文手册 章 8. 数据类型
Array Functions and Operators
git init
git remote add origin repository-url
git push --set-upstream origin master
git remote set-url origin new-repository-url
git checkout <hash> <filename>
git archive -o ../updated.zip HEAD $(git diff --name-only HEAD^)
1 | git rm -r -n --cached docs/api # -n 预览命令,并不真正删除 |
git cherry-pick <commit id>
List的成员函数,所以仅适用list排序,原址排序
1 | sort(...) |
示例:
1 | list_a = [26, 2, 10, 20, 11, 8] |
内建函数,适用于任何可迭代对象,返回排序后的对象
1 | sorted(...) |
示例:
1 | iter_a = [dict(num=26),dict(num=2),dict(num=10),dict(num=20),dict(num=11),dict(num=8)] |
参考资料:
Python中sorted函数的用法
以下示例: 父项目为box-server,子项目为box-deploy
cd box-server
git submodule add box-deploy-repository-url deploy
git ci -m 'add submodule deploy'
git clone --recursive box-server-repository-url
git clone box-server-repository-url
git submodule init
git submodule update
git clone box-server-repository-url
git submodule update --init --recursive