postgresql中原来的数据结构类型如下:
我想把 in_instance_id 类型有int4 修改为:serial4 结构报如下错误:
这种问题其实是,serial不是一个真实的数据类型,只能在新建表的时候使用,如果中间更新类型就会报如上错误:
这里从stackoverflow 截取一段问题答案:
This happened because you may use serial data type only when you are creating new table or adding new column to a table. If you'll try to ALTER existing table using this data type you'll get an error. Because serial is not a true data type, but merely an abbreviation or alias for a bit longer query.
发生这种情况是因为只有在创建新表或向表中添加新列时才可以使用串行数据类型。如果尝试使用此数据类型更改现有表,则会收到错误消息。因为serial不是真正的数据类型,而只是用于更长查询的缩写或别名
In case you would like to reach the same effect, as you are expecting from using serial data type when you are altering existing table you may do this:
如果您希望达到相同的效果,正如您期望的那样,在更改现有表时使用串行数据类型,可以这样做:
CREATE SEQUENCE my_serial AS integer START1 OWNED BY address.new_id;ALTERTABLE address ALTERCOLUMN new_id SETDEFAULT nextval('my_serial');
First line of query creates your own sequence with name my_serial. OWEND BY statement connects newly created sequence with exact column of your table. In your exact case table is address and column is new_id. START statement defines what value should this sequence start from.
查询的第一行创建名为my_serial的自己的序列。 OWEND BY语句将新创建的序列与表的确切列相连接。在您的确切情况下,表是地址,列是new_id。 START语句定义此序列应从哪个值开始
Second line alters your table with new default value, which will be determined by previously created sequence.
It will brings you to the same result as you were expecting from using serial.
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!