我的应用程序使用光滑(V2.0.0)来管理一个PostgreSQL(9.1)数据库。
我的一个表中包含网络设备,因此Mac和IP的地址。 我用的Postgres类型macaddr
和inet
,因为他们似乎是任务的理想工具。 现在我想用滑溜溜的解除emebedding但很难定义我的表。 当我自动生成的代码为我的表,我注意到String
来代替那些类型,但并不介意。
这工作正常从数据库读取,但是当我尝试更新或插入一行它会导致
org.postgresql.util.PSQLException: ERROR:
column "mac" is of type macaddr but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
至极似乎很合乎逻辑。
现在的问题是我怎么能告诉滑头说:
- 这collumn实际上是一个类型的
macaddr
/inet
要么
- 这collumn需要插入之前铸造,但可以以其他方式被视为一个字符串
?
更新:
克雷格说明我已经创建了周围使用包装函数隐式转换macaddr_in
和inet_in
\dC macaddr
List of casts
Source type | Target type | Function | Implicit?
-------------+-------------+----------------+-----------
text | macaddr | macaddr_intext | yes
\dC inet
List of casts
Source type | Target type | Function | Implicit?
-------------+-------------------+--------------------+---------------
cidr | inet | (binary coercible) | yes
inet | character | text | in assignment
inet | character varying | text | in assignment
inet | cidr | cidr | in assignment
inet | text | text | in assignment
text | inet | inet_intext | yes
\df+ macaddr_intext
List of functions
Schema | Name | Result data type | Argument data types | Type | Volatility | Owner | Language | Source code | Description
--------+----------------+------------------+---------------------+--------+------------+----------+----------+---------------------------------+-------------
public | macaddr_intext | macaddr | text | normal | immutable | postgres | sql | |
: select macaddr_in($1::cstring);
:
\df+ inet_intext
List of functions
Schema | Name | Result data type | Argument data types | Type | Volatility | Owner | Language | Source code | Description
--------+-------------+------------------+---------------------+--------+------------+----------+----------+------------------------------+-------------
public | inet_intext | inet | text | normal | immutable | postgres | sql | |
: select inet_in($1::cstring);
:
ERRORMESSAGE仍然是完全一样的,如上所示。
Commads重现:
在psql <tablename>
:
create or replace function macaddr_intext(text) returns macaddr as $$
select macaddr_in($1::cstring);
$$ language sql immutable;
create cast (text as macaddr) with function macaddr_intext(text) as implicit;
create or replace function inet_intext(text) returns inet as $$
select inet_in($1::cstring);
$$ language sql immutable;
create cast (text as inet) with function inet_intext(text) as implicit;
UPDATE2:
我把范围缩小到一个权限错误,因为如果我作为用户身份运行postgres
:
mydb=# create table test(i inet, m macaddr)
CREATE TABLE
mydb=# insert into test values ('1.1.1.1'::text, '00:00:00:00:00:00'::text);
INSERT 0 1
但如果我尝试运行它实际上试图插入用户
mydb=> insert into test values ('1.1.1.1'::text, '00:00:00:00:00:00'::text);
ERROR: permission denied for relation test
建立数据库时我已经运行:
template1=# GRANT ALL PRIVILEGES ON DATABASE mydb to myuser;
UPDATE3:
UPDATE2横空出世,因为所创建的表是由拥有的只是问题postgres
,而不是myuser