[Truffle] TypeError: Dynamically-sized keys for public mappings are not supported

Hi everyone.

I am trying to write a smart contract for a simple car hire system and I am stuck with an issue. I am using a nested mapping to keep track of all car previous users. The contract is compiled and tested successfully on Remix IDE (JavaScript VM) but when I try to compile it on Truffle I got this error:

TypeError: Dynamically-sized keys for public mappings are not supported.
    mapping  (string => mapping(address => CarHireRecord )) public record ;
    ^-------------------------------------------------------------------^

This is the contract

pragma solidity 0.5.11;

contract SimpleCarHire {

mapping  (string => mapping(address => CarHireRecord )) public record ;
mapping  (string => address[]) carUsers;

struct CarHireRecord{
    uint startDate;
    uint endDate;
}

function getCarUsers(string memory _vehicleRegistrationNumber) public view returns(address[] memory) {
    
    return(carUsers[_vehicleRegistrationNumber]);
}

function hireCar( string memory _vehicleRegistrationNumber, uint _end) public {
    
    record[_vehicleRegistrationNumber][msg.sender].startDate = now ;
    record[_vehicleRegistrationNumber][msg.sender].endDate = now + _end;
    carUsers[_vehicleRegistrationNumber].push(msg.sender);
}

}

Are you sure you don’t have a version mismatch for Solidity between Remix and Truffle?

By default Truffle uses Solidity 0.5.0, unless you specify otherwise in truffle-config.js.

Yes, you are right. I just added compiler version in truffle-config.js :wink:

Awesome Faisal, glad you found the error :slight_smile: