When using the complex data structure of mapping+struct in solidity, the problem encountered is "Mappings cannot be assigned to".
More blockchain technology and application classification:
Blockchain applicationBlockchain development
Ethernet Square | Fabric | BCOS | cryptography | consensus algorithm | bitcoin | Other chain
Token Economy | Traditional Financial Scenarios | Decentralized Finance | Anti-counterfeiting Traceability | Data Sharing | Trusted Deposit
Error code
Code
constructor() public{
dataBases[TypeConvertUtil.stringToBytes32(TABLE_BAG_NAME)] = bagRecordOf;
dataBases[TypeConvertUtil.stringToBytes32(TABLE_LOCATIONS_NAME)] = locationRecordOf;
dataBases[TypeConvertUtil.stringToBytes32(TABLE_ATTENDLOCATION_NAME)] = attendLocationRecordOf;
}
Writing in this way will cause the following error:
browser/TableDefTools.sol:112:9: TypeError: Mappings cannot be assigned to.
dataBases[TypeConvertUtil.stringToBytes32(TABLE_BAG_NAME)] = bagRecordOf;
^----------------------------------------------------------------------^
Correct code
Define the structure:
struct RecordBean {
// 表名称
string tableName;
// 主键值
string primaryKey;
// 内容值
string[] values;
}
mapping(bytes32 => mapping(bytes32 => RecordBean)) dataBases;
Assignment:
dataBases[TypeConvertUtil.stringToBytes32(_tableName)][TypeConvertUtil.stringToBytes32(_primaryKey)] = recordBean;
Value:
RecordBean recordBean = dataBases[TypeConvertUtil.stringToBytes32(_tableName)][TypeConvertUtil.stringToBytes32(_primaryKey)];
Original link: Solidity uses mapping+struct complex data structure